Performance of rand() in CUDA host function [closed]
Why is this function that uses rand()
slow and what approach would have better performance when used in.cu
file?
In my case I would like to replace one function that reads strings from file and fills an array(data) to other function which generate strings and fills the same array. Then this array transfers from host to device.
Would like to replace this function that reads file and fills array:
char* get_data(char* file_name) {
FILE* f;
if (!(f = fopen(file_name, "r")))
{
printf("Error opening file %s", file_name);
exit(1);
}
char* data = (char*)malloc(sizeof(char) * count * str_length);
if (data == NULL)
{
perror("Error allocating memory for list of Strings.\n");
exit(1);
}
int index = 0;
char buf[50];
while (1)
{
if (fgets(buf, str_length + 1, f) == NULL)
break;
buf[strlen(buf) - 1] = '\0';
memcpy(&data[index], buf, str_length);
index += str_length - 1;
}
return data;
}
To this, that generate strings and fills array:
char* get_data()
{
char* data = (char*)calloc(count * str_length, sizeof(char*));
for (int c = 0; c < count; c++) {
int index = 0;
char buf[str_length] = "";
for (int i = 0; i < (str_length - 1); i++)
{
buf[i] = charset[rand() % charset_size];
}
strcat(&data[index], buf);
index += str_length + 1;
}
return data;
}
Function "get_data" which use rand() and generates strings works well, but very slow. Performance about 10k strings per second (each string 20 chars long).
Full compillable code posted here - https://pastebin.com/AJrPL7bU
Dear moderator. I have reworked the question. Full code code has only minimal (55 lines) repetitive\compillable code. Tha main question is why my function slow. Thanks.
from Recent Questions - Stack Overflow https://ift.tt/3Fyu0vr
https://ift.tt/eA8V8J
Comments
Post a Comment