Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void createMemObjects(void){
- data = (float *) malloc (N * sizeof (float));//Make space for N number of floats
- results = (float *) malloc (sizeof (float));
- /* Fill the vector with float values. */
- for (unsigned i = 0; i < N; i++){
- data[i] = 1;
- printf("%d: %d\n", i, (int) data[i]);
- }
- /* Create the device memory VARIABLES. */
- input = clCreateBuffer (context, CL_MEM_READ_ONLY,
- sizeof (float) * N, NULL, NULL);
- output = clCreateBuffer (context, CL_MEM_WRITE_ONLY,
- sizeof (float), NULL, NULL);
- if (!input || !output)
- die ("Error: Failed to allocate device memory!");
- }
- /* Transfer the input vector into device memory. */
- void transferToDevice(void){
- if (CL_SUCCESS
- != clEnqueueWriteBuffer (commands, input,
- CL_TRUE, 0, sizeof (float) * N,
- data, 0, NULL, NULL))
- die ("Error: Failed to write to source array!");
- }
- /* Set the arguments to the compute kernel. */
- void setKernelArguments(void){
- err = 0;
- err = clSetKernelArg (kernel, 0, sizeof (cl_mem), &input);
- err |= clSetKernelArg (kernel, 1, sizeof (cl_mem), &output);
- err |= clSetKernelArg (kernel, 2, sizeof (unsigned int), &N);
- if (err != CL_SUCCESS)
- die ("Error: Failed to set kernel arguments!");
- }
- void runKernel(void){
- /* Execute the kernel over the vector using the
- maximum number of work group items for this device. */
- global = N;
- if (CL_SUCCESS
- != clEnqueueNDRangeKernel (commands, kernel,
- 1, NULL, &global, NULL, 0, NULL, NULL))
- die ("Error: Failed to execute kernel!");
- /* Wait for all commands to complete. */
- clFinish (commands);
- }
- /* Read back the results from the device to verify the output. */
- void transferToHost(void){
- if (CL_SUCCESS
- != clEnqueueReadBuffer (commands, output,
- CL_TRUE, 0, sizeof (float),
- results, 0, NULL, NULL))
- die ("Error: Failed to read output array!");
- printf("Results %d\n", (int)*results);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement