Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read the binary file which is kernel.aocx from the host code. But, I got a segmentation fault. I understand that segmentation fault caused because of accessing memory that does not belong to me. But I am not sure why this happened in my code. Lines of codes for reading binary file are:

 size_t length = 0x10000000;
unsigned char* binary = (unsigned char*)malloc(length);
FILE* fp = fopen("kernel.aocx", "rb");
fread(binary, length, 1, fp);
fclose(fp);


kernelcode:
inline float Euclidean_distance(__global int* restrict array_point_A, __global int* restrict array_point_B) {
    float sum = 0.0;
    float  w[20] = { 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 (int i = 0; i < 20; ++i) {
        float a = array_point_A[i] - array_point_B[i];
        float wieghted_distance = w[i] * (a * a);
        sum += wieghted_distance;

    }
    return sqrt(sum);
}
__kernel void KNN_classifier(__global int* restrict X_train, __global int* restrict Y_train, __global int* restrict data_point, __global int* restrict array_X_set, __global int* restrict index_arr, __global float* restrict array_dist)
{
    int k = 3;
    for (int i = 0; i < 4344; ++i) {
        for (int j = 0; j < 20; ++j) {
            data_point[j] = array_X_set[i];
        }
        array_dist[i] = Euclidean_distance(X_train, data_point);
        index_arr[i] = i;
    }
    int x;
    for (int i = 0; i < 4344; i++)
    {
        for (int j = i + 1; j < 4344; j++)
        {
            if (array_dist[index_arr[i]] > array_dist[index_arr[j]])
            {
                x = index_arr[i];
                index_arr[i] = index_arr[j];
                index_arr[j] = x;
            }

        }
    }

    int array_Y_class_target[2] = {};
    float CT[2] = {};
    float SumOf_Each_class_distances[2] = { 0.0 };
    int min_index = -1;
    for (int i = 0; i < 4344; ++i) {
        for (int i = k; i > 0; --i) {
            for (int c = 0; c < 2; ++c) {
                for (int j = 0; j < i; ++j) {
                    int index = index_arr[j];
                    if (Y_train[index] == c)
                    {
                        array_Y_class_target[c] ++;
                        float dist = array_dist[index_arr[j]];
                        SumOf_Each_class_distances[c] += dist;
                    }
                }
                if (array_Y_class_target[c] != 0)
                {
                    CT[c] = (((float)k / (float)array_Y_class_target[c]) + (SumOf_Each_class_distances[c] / (float)array_Y_class_target[c]));
                }
                else
                {
                    CT[c] = 1.5;

                }
            }

            float min = 1.8;
            int max_index = -1;
            for (int r = 0; r < 2; ++r) {
                float elem = CT[r];
                if (elem <= min) {
                    min = elem;
                    min_index = r;
                }
            }
            for (size_t r = 0; r < 2; ++r) {
                float elem = CT[r];
                if ((elem == min) && (r != min_index)) {
                    if (SumOf_Each_class_distances[0] < SumOf_Each_class_distances[1])
                    {
                        min_index = 0;
                    }
                    else
                    {
                        min_index = 1;
                    }
                }

            }
            index_arr[i] = min_index; ;
        }
    }
}


What I have tried:

Not sure what is exact problem in my code
Posted
Comments
Richard MacCutchan 14-Oct-21 9:19am    
"Not sure what is exact problem in my code"
Nor me, since you have not explained where the segmentation fault occurs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900