Write a program using OpenCL to do the following: Host code. O Create two STL vectors, named vecl and vec2. Initialise their contents as follows: vecl: A vector of ints that contains 32 elements. Initialise the elements with random values between 10 and 20. vec2: A vector of ints that contains 16 elements. Initialise the first half of the vector with values from 2 to 9 and the second half with values from -9 to -2. Create and initialise the necessary memory objects to pass vecl and vec2 as input to the kernel, and another memory object for kernel output. o Enqueue the kernel such that each work-item will process 8-elements from vecl, respectively, i.e. work-item 1 will refer to elements 0-7, work-item 2 will refer to elements 8-15, etc. After kernel execution, obtain the output from the kernel and display the results on O o screen.
int in_vec_size = 100;
int out_vec_size = 100;
vector<vector<float>> in_vec(10,vector<float>(10));
vector<vector<float>> out_vec(10, vector<float>(10));
int k = 0;
//initialize the input vec
for (int i=0; i < 10;i++)
{
for (int j = 0; j < 10;j++)
{
in_vec[i][j] = k++;
out_vec[i][j] = 0;
}
}
//creating bufferes
cl::Buffer inBuff(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, in_vec_size*4, &in_vec[0][0]);
cl::Buffer outBuff(context, CL_MEM_WRITE_ONLY, out_vec_size*4, NULL);
//set kernal args
kernal.setArg(0, inBuff);
kernal.setArg(1, outBuff);
kernal.setArg(2, in_vec_size);
cl::CommandQueue queue(context, devices_gpu[0]);
queue.enqueueTask(kernal);
queue.enqueueWriteBuffer(inBuff, CL_TRUE, 0, in_vec_size*4, &in_vec[0][0]);
queue.enqueueReadBuffer(outBuff, CL_TRUE, 0, out_vec_size*4, &out_vec[0][0]);
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << out_vec[i][j] << endl;
}
}
__kernel void add(__global float*in,__global float*out,int x)
{
// i=get_global_id(0);
for(int i=0;i<x;i++)
{
out[i] = in[i]+10;
}
}
Comments
Dear jordan,if you have serious assignment that requires large amount
of work and hence cannot be done for free you can submit it as assignment and our
experts will surely assist you.
kindly provide full source code it's giving errors.
Leave a comment