Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 7th, 2012  |  syntax: C  |  size: 3.35 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "cl.h"
  2. #include "stdlib.h"
  3. #include "stdio.h"
  4. #include "string.h"
  5. #include  <sys/time.h>
  6. #include <sys/mman.h>
  7.  
  8. #define NUM_OF_EXEC 2
  9. #define PAGE_SIZE 4096
  10. #define HOST_BUFF_SIZE 2048
  11. #define PAGE_MASK 0xFFFFF000
  12. #define BUFF_SIZE 1024
  13.  
  14. int host_buff [HOST_BUFF_SIZE];
  15.  
  16. int
  17. main (int argc, char ** argv)
  18. {
  19.         int error;
  20.         struct timeval tm1;
  21.         struct timeval tm2;
  22.         cl_event main_event[2*NUM_OF_EXEC];
  23.         int i;
  24.         int offset = PAGE_SIZE - ((unsigned long)host_buff) % PAGE_SIZE;
  25.         void * ptr = (void*)host_buff + offset;
  26.  
  27.         unsigned int *buf, *buf2;
  28.         buf = (unsigned int *) malloc(BUFF_SIZE*4 + PAGE_SIZE);
  29.         buf2 = (unsigned int*) malloc(BUFF_SIZE*4 + PAGE_SIZE);
  30.        
  31.         //Some page-aligning
  32.         unsigned long page_addr, page_addr2;
  33.         unsigned long page_offset, page_offset2;
  34.         int wset, rset;
  35.  
  36.         page_addr = (unsigned long) (&buf[0]) & PAGE_MASK;
  37.         page_addr2 = (unsigned long) (&buf2[0]) & PAGE_MASK;
  38.     page_offset = (unsigned long) &buf[0] - page_addr;
  39.     page_offset2 = (unsigned long) &buf2[0] - page_addr2;
  40.  
  41.         wset = (4096 - ((int) page_offset)) >> 2;
  42.         rset = (4096 - ((int) page_offset2)) >> 2;
  43.  
  44.         for (i = 0; i < BUFF_SIZE; ++i)
  45.         {
  46.                 buf[i+wset] = i;
  47.                 buf2[i+rset] = 2*i;
  48.         }
  49.  
  50.  
  51.         cl_context context = clCreateContextFromType(NULL, CL_DEVICE_TYPE_FPGA, NULL, NULL, &error);
  52.         if (context == NULL)
  53.         {
  54.                 printf("Context creation failed (%d)\n", error);
  55.                 return -1;
  56.         }
  57.         printf("Context created\n");
  58.  
  59.         cl_device_id dev;
  60.         cl_uint num_of_devs;
  61.  
  62.         if ((error = clGetDeviceIDs(0,CL_DEVICE_TYPE_FPGA,1, &dev, &num_of_devs)) != CL_SUCCESS || num_of_devs == 0)
  63.         {
  64.                 printf ("Can not get device ids num_of_devs = %d, error = %d\n", num_of_devs, error);
  65.                 return -1;
  66.         }
  67.         printf("Device selected\n");
  68.  
  69.         cl_command_queue queue = clCreateCommandQueue(context, dev, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &error);
  70.         if (queue == NULL)
  71.         {
  72.                 printf("Command queue creation failed (%d)\n", error);
  73.                 return -1;
  74.         }
  75.         printf("Command queue created\n");
  76.  
  77.         cl_mem buff = clCreateBuffer(context, CL_MEM_READ_WRITE,  PAGE_SIZE, NULL, &error);
  78.         if (buff == NULL)
  79.         {
  80.                 printf("Memory buffer creation failed (%d)\n", error);
  81.                 return -1;
  82.         }
  83.         printf("Memory buffer created\n");
  84.  
  85.  
  86.         gettimeofday (&tm1, NULL);
  87.         for (i = 0; i < NUM_OF_EXEC; i ++)
  88.         {
  89.                 clEnqueueWriteBuffer(queue, buff, CL_FALSE, 0, 4096, &buf[wset], 0, NULL, &main_event[2*i]);
  90.                 clEnqueueReadBuffer(queue, buff, CL_FALSE, 0, 4096, &buf2[rset], 0, NULL, &main_event[2*i+1]);
  91.         }
  92.         gettimeofday (&tm2, NULL);
  93.         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);
  94.  
  95.         sleep (1);
  96.  
  97.         gettimeofday (&tm1, NULL);
  98.         clWaitForEvents (2*NUM_OF_EXEC, &main_event[0]);
  99.         gettimeofday (&tm2, NULL);
  100.         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);
  101.  
  102.         for (i = 0; i < BUFF_SIZE; i ++)
  103.                 if (buf2[i+rset] != i)
  104.                 {
  105.                         printf ("Error in buf2 [%d] = %d\n", i, buf2[i+rset]);
  106.                         return -1;
  107.                 }
  108.         printf("Everything is ok\n");
  109.  
  110.         error = clReleaseCommandQueue(queue);
  111.         if (error != CL_SUCCESS)
  112.         {
  113.                 printf("can not release command queue (%d)\n", error);
  114.         }
  115.         printf ("Command queue released\n");
  116.  
  117.  
  118.         error = clReleaseContext(context);
  119.         if (error != CL_SUCCESS)
  120.         {
  121.                 printf("can not release context (%d)\n", error);
  122.         }
  123.         printf ("Context released\n");
  124.  
  125.         return 0;
  126. }
clone this paste RAW Paste Data