#include "cl.h" #include "stdlib.h" #include "stdio.h" #include "string.h" #include #include #define NUM_OF_EXEC 2 #define PAGE_SIZE 4096 #define HOST_BUFF_SIZE 2048 #define PAGE_MASK 0xFFFFF000 #define BUFF_SIZE 1024 int host_buff [HOST_BUFF_SIZE]; int main (int argc, char ** argv) { int error; struct timeval tm1; struct timeval tm2; cl_event main_event[2*NUM_OF_EXEC]; int i; int offset = PAGE_SIZE - ((unsigned long)host_buff) % PAGE_SIZE; void * ptr = (void*)host_buff + offset; unsigned int *buf, *buf2; buf = (unsigned int *) malloc(BUFF_SIZE*4 + PAGE_SIZE); buf2 = (unsigned int*) malloc(BUFF_SIZE*4 + PAGE_SIZE); //Some page-aligning unsigned long page_addr, page_addr2; unsigned long page_offset, page_offset2; int wset, rset; page_addr = (unsigned long) (&buf[0]) & PAGE_MASK; page_addr2 = (unsigned long) (&buf2[0]) & PAGE_MASK; page_offset = (unsigned long) &buf[0] - page_addr; page_offset2 = (unsigned long) &buf2[0] - page_addr2; wset = (4096 - ((int) page_offset)) >> 2; rset = (4096 - ((int) page_offset2)) >> 2; for (i = 0; i < BUFF_SIZE; ++i) { buf[i+wset] = i; buf2[i+rset] = 2*i; } cl_context context = clCreateContextFromType(NULL, CL_DEVICE_TYPE_FPGA, NULL, NULL, &error); if (context == NULL) { printf("Context creation failed (%d)\n", error); return -1; } printf("Context created\n"); cl_device_id dev; cl_uint num_of_devs; if ((error = clGetDeviceIDs(0,CL_DEVICE_TYPE_FPGA,1, &dev, &num_of_devs)) != CL_SUCCESS || num_of_devs == 0) { printf ("Can not get device ids num_of_devs = %d, error = %d\n", num_of_devs, error); return -1; } printf("Device selected\n"); cl_command_queue queue = clCreateCommandQueue(context, dev, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &error); if (queue == NULL) { printf("Command queue creation failed (%d)\n", error); return -1; } printf("Command queue created\n"); cl_mem buff = clCreateBuffer(context, CL_MEM_READ_WRITE, PAGE_SIZE, NULL, &error); if (buff == NULL) { printf("Memory buffer creation failed (%d)\n", error); return -1; } printf("Memory buffer created\n"); gettimeofday (&tm1, NULL); for (i = 0; i < NUM_OF_EXEC; i ++) { clEnqueueWriteBuffer(queue, buff, CL_FALSE, 0, 4096, &buf[wset], 0, NULL, &main_event[2*i]); clEnqueueReadBuffer(queue, buff, CL_FALSE, 0, 4096, &buf2[rset], 0, NULL, &main_event[2*i+1]); } gettimeofday (&tm2, NULL); printf ("Time to start %d one page dma writes: %d %d\n", NUM_OF_EXEC, tm2.tv_sec - tm1.tv_sec, tm2.tv_usec - tm1.tv_usec); sleep (1); gettimeofday (&tm1, NULL); clWaitForEvents (2*NUM_OF_EXEC, &main_event[0]); gettimeofday (&tm2, NULL); printf ("Time to wait %d completed events: %d %d\n", NUM_OF_EXEC, tm2.tv_sec - tm1.tv_sec, tm2.tv_usec - tm1.tv_usec); for (i = 0; i < BUFF_SIZE; i ++) if (buf2[i+rset] != i) { printf ("Error in buf2 [%d] = %d\n", i, buf2[i+rset]); return -1; } printf("Everything is ok\n"); error = clReleaseCommandQueue(queue); if (error != CL_SUCCESS) { printf("can not release command queue (%d)\n", error); } printf ("Command queue released\n"); error = clReleaseContext(context); if (error != CL_SUCCESS) { printf("can not release context (%d)\n", error); } printf ("Context released\n"); return 0; }