Hi,
I am a newbie to CUDA C/C++. I am trying to copy array of strings to device and copy it back host.I am using following code.I am getting segmentation fault and I am not able to decipher it.Here is the code that I am trying.
char *newdict[dict.size()]; /* dict is a conventional string vector containing words from a file.
char *device_dict[dict.size()];
for(int i=0;i<dict.size();i++)
{
cudaMalloc((void**)&device_dict[i],strlen(str[i])*sizeof(char));
}
cout<<"Device memory allocated-->"<<endl;
for(int i=0;i<dict.size();i++)
{
cudaMemcpy(device_dict[i],str[i],strlen(str[i])*sizeof(char),cudaMemcpyHostToDevice);
}
cout<<"Copied to Device!!"<<endl;
//for(int i=0;i<dict.size();i++)
// {
cudaMemcpy(newdict[i],device_dict[i],strlen(str[i])*sizeof(char),cudaMemcpyDeviceToHost);
// }
cout<<"Copied back to Host!!"<<endl;
cout<<endl;
cout<<newdict[1]<<newdict[2];
for(int i=0;i<dict.size();i++)
free(str[i]);
free(newdict[i]);
cudaFree(device_dict[i]);
}
cout<<endl<<"Freed"<<endl;
I am not sure where I am going wrong.I think applying for loop to cudaMemcpy is causing the problem but I just need an expert opinion as to why this shouldnt be done and what is the alternate way to such transfers from host to device and vice versa.
Any help will be appreciated!
Thanks and Advance.