I am asked to implement the following part of code into kernel code. Actually, I have tried but not sure about thestd::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 rowsinline 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++; }1 Answer1
Review
Welcome to Code Review. There are some suggestions as follows.
Const Definition
You already define the valueN1 andN2, 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 in20 here. How about the case of the dimension which is different from20?
The weightw inEuclidean_distance function
Do you have any reason to do this? In general, thisw array seems to be a kind of data, which could be passed as a parameter likeEuclidean_distance(array<int, 20>& array_point_A, array<int, 20>& array_point_B, array<int, 20>& w) instead and you provide the value ofw parameter when you call it. It is not necessary to define theconst data in a function.
You mustlog in to answer this question.
Explore related questions
See similar questions with these tags.

