Skip to content

Instantly share code, notes, and snippets.

@nicolamontecchio
Created February 11, 2011 16:33
Show Gist options
  • Select an option

  • Save nicolamontecchio/822597 to your computer and use it in GitHub Desktop.

Select an option

Save nicolamontecchio/822597 to your computer and use it in GitHub Desktop.
OPENCL - print a cl_mem vector to stdout; vector type is passed as template
// print out a vector of type T on stdout
template <typename T>
void printClVector(cl_mem &clVector, int length, cl_command_queue &commands, int printrowlen = -1)
{
T *tmp = new T[length];
int err = clEnqueueReadBuffer(commands, clVector, CL_TRUE, 0, sizeof(T) * length, tmp, 0, NULL, NULL );
if (err != CL_SUCCESS)
{
printf("Error: Failed to read output array! %d\n", err);
exit(1);
}
if(printrowlen < 0) // print all as one line
{
for(int k = 0; k < length; k++)
{
cout << tmp[k] << " ";
}
cout << endl;
}
else // print rows of "printrowlen" length
{
for(int k = 0; k < length/printrowlen; k++)
{
for(int j = 0; j < printrowlen; j++)
{
cout << tmp[k*printrowlen + j] << " ";
}
cout << endl;
}
cout << endl;
}
delete[] tmp;
}
// example call:
// printClVector<float>(gpu_cluster_centroids, 2*K, commands, 2);
// where gpu_cluster_centroids holds 2K floats, and you print 2 values per line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment