Home > Blockchain >  How do I locate the data of host and kernel in two different locations
How do I locate the data of host and kernel in two different locations

Time:09-17

I have wrote the kernel code that will be run on FPGA device. Currently, I am writing the host code which will be run on CPU. In the last meeting with my professor, he told me that the data (arrays in my case) in the host should not be located in the same memory space of that for the device. Thus, he asked me to use pointers for the data in the host part.

Actually, 1- I am not sure if I understand why the data should be in two different location.

2- how to use pointers to use different memory location.

3-bellow are the arrays in the host. Any idea how to convert these arrays into pointers?

int array_X_set[5430][20];
int array_Y_set[5430];

for (int i = 0; i < 5430; i  ) {
    for (int j = 0; j < 20; j  )
        array_X_set[i][j] = array_X_dataset[i][j];
    array_Y_set[i] = array_Y_dataset[i];
}

int X_train[4344][20] = {};
int Y_train[4344] = {};
int X_test[1086][20] = {};
int Y_test[1086] = {};

CodePudding user response:

You currently have allocated your arrays on the stack. This limits the maximum error size. It is better to use heap allocation with (1-dimensional) pointers:

int* array_X_set = new int[5430*20]; // linearize 2D array
int* array_Y_set = new int[5430];

for (int i = 0; i < 5430; i  ) {
    for (int j = 0; j < 20; j  )
        array_X_set[i j*5430] = array_X_dataset[i j*5430];
    array_Y_set[i] = array_Y_dataset[i];
}

int* X_train = new int[4344*20];
int* Y_train = new int[4344];
int* X_test = new int[1086*20];
int* Y_test = new int[1086];

At the end of your code, don't forget to free the allocated memory again. For every new[] there must be one corresponding delete[]:

delete[] array_X_set;
delete[] array_Y_set;

delete[] X_train;
delete[] Y_train;
delete[] X_test;
delete[] Y_test;

All these arays are on the host (CPU) side in main system memory. On your device (FPGA), you need to allocate the same amount of (device) memory as OpenCL Buffer objects. Then you can copy the data over with enqueueWriteBuffer() and enqueueReadBuffer().

  • Related