Advertisement
Guest User

Untitled

a guest
Nov 6th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. void createMemObjects(void){
  2.    
  3.     data = (float *) malloc (N * sizeof (float));//Make space for N number of floats
  4.     results = (float *) malloc (sizeof (float));
  5.    
  6.     /* Fill the vector with float values.  */
  7.     for (unsigned i = 0; i < N; i++){
  8.         data[i] = 1;
  9.         printf("%d: %d\n", i, (int) data[i]);
  10.     }
  11.    
  12.    
  13.     /* Create the device memory VARIABLES.  */
  14.     input = clCreateBuffer (context, CL_MEM_READ_ONLY,
  15.                             sizeof (float) * N, NULL, NULL);
  16.     output = clCreateBuffer (context, CL_MEM_WRITE_ONLY,
  17.                              sizeof (float), NULL, NULL);
  18.     if (!input || !output)
  19.         die ("Error: Failed to allocate device memory!");
  20. }
  21.  
  22.  
  23. /* Transfer the input vector into device memory.  */
  24. void transferToDevice(void){
  25.     if (CL_SUCCESS
  26.         != clEnqueueWriteBuffer (commands, input,
  27.                                  CL_TRUE, 0, sizeof (float) * N,
  28.                                  data, 0, NULL, NULL))
  29.         die ("Error: Failed to write to source array!");
  30. }
  31.  
  32.  
  33. /* Set the arguments to the compute kernel.  */
  34. void setKernelArguments(void){
  35.     err = 0;
  36.     err = clSetKernelArg (kernel, 0, sizeof (cl_mem), &input);
  37.     err |= clSetKernelArg (kernel, 1, sizeof (cl_mem), &output);
  38.     err |= clSetKernelArg (kernel, 2, sizeof (unsigned int), &N);
  39.    
  40.     if (err != CL_SUCCESS)
  41.         die ("Error: Failed to set kernel arguments!");
  42. }
  43.  
  44. void runKernel(void){
  45.    
  46.     /* Execute the kernel over the vector using the
  47.      maximum number of work group items for this device.  */
  48.     global = N;
  49.     if (CL_SUCCESS
  50.         != clEnqueueNDRangeKernel (commands, kernel,
  51.                                    1, NULL, &global, NULL, 0, NULL, NULL))
  52.         die ("Error: Failed to execute kernel!");
  53.    
  54.     /* Wait for all commands to complete.  */
  55.     clFinish (commands);
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62. /* Read back the results from the device to verify the output.  */
  63. void transferToHost(void){
  64.     if (CL_SUCCESS
  65.         != clEnqueueReadBuffer (commands, output,
  66.                                 CL_TRUE, 0, sizeof (float),
  67.                                 results, 0, NULL, NULL))
  68.         die ("Error: Failed to read output array!");
  69.     printf("Results %d\n", (int)*results);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement