0
\$\begingroup\$

I am asked to implement the following part of code into kernel code. Actually, I have tried but not sure about the std::array.

This is the original code for the function.

int KNN_classifier(array<array<int, 20>, 4344>& array_X_train, array<int, 4344>& array_Y_train,
    int k, array<int, 20>& data_point) {


    // Calculate the distance between data_point and all points.
   int index = 0;
   static array<pair<int, float>, 4344> array_dist{};
    pair<int, float> pair_tmp;
    //Apply uroll:__attribute__((opencl_unroll_hint(n)))
    for (size_t i = 0; i < array_X_train.size(); ++i) {
        pair_tmp = make_pair(i, Euclidean_distance(array_X_train[i], data_point));
        //  dist_vec.push_back(pair_tmp);
        array_dist[index] = pair_tmp;
        index++;       

    }

This is the kernel code:

  #define N1 20 //num of cols
#define N2 4344 //num of rows

inline float Euclidean_distance(array<int, 20>& array_point_A, array<int, 20>& array_point_B) {
    float sum = 0.0;
    static array<float, 20> w = { 0.0847282, 0.0408621, 0.105036, 0.0619821, 0.0595455, 0.0416739, 0.0181147, 0.00592921,
     0.040049, 0.0766054, 0.0441091, 0.0376111, 0.0124285, 0.0733558, 0.0587338, 0.0303001, 0.0579207, 0.0449221,
          0.0530462, 0.0530462 };
     for (size_t i = 0; i < array_point_A.size(); ++i) {
        int a = array_point_A[i] - array_point_B[i];
        float wieghted_distance = w[i] * (static_cast<float> (a) * static_cast<float> (a)); 
        sum += wieghted_distance;

    }
    return sqrt(sum);
}


__kernel int  KNN_classifier(__global const int* restrict array_X_train, __global const int* restrict array_Y_train,
    __global const int* restrict data_point, int k)
{
    int index = 0;
    static array<pair<int, float>, 4344> array_dist{};
    pair<int, float> pair_tmp;
    for (size_t i = 0; i < array_X_train.size(); ++i) {
        pair_tmp = make_pair(i, Euclidean_distance(array_X_train[i], data_point));
        array_dist[index] = pair_tmp;
        index++;
    }
\$\endgroup\$
0
\$\begingroup\$

Review

Welcome to Code Review. There are some suggestions as follows.

Const Definition

You already define the value N1 and N2, why not use that in the array template parameter directly (e.g. array<float, N1>)? Actually, not sure what kind of problem you want to solve with KNN algorithm, you specified the dimension of KNN algorithm in 20 here. How about the case of the dimension which is different from 20?

The weight w in Euclidean_distance function

Do you have any reason to do this? In general, this w array seems to be a kind of data, which could be passed as a parameter like Euclidean_distance(array<int, 20>& array_point_A, array<int, 20>& array_point_B, array<int, 20>& w) instead and you provide the value of w parameter when you call it. It is not necessary to define the const data in a function.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.