0
我想在C中設計一個小程序。我使用openMP來並行化部分代碼,它通過逐行讀取文件,存儲大量的行讀取數組緩衝區並調用一個函數,該函數現在'等待'10微秒。這是我的代碼。 我基本上想要並行執行該特定功能。 即緩衝區的數據一旦被填滿就被分配到單個線程上,然後第二個緩衝區填滿第二個線程,等等......。這可能不同步,並且不需要在此階段。使用openmp並行函數調用
int count = 0; //global
char *array_buffer[8]; //global
char process(int lent)
{
int row,col;
#pragma omp single
for(row = 0; row<count; row++)
{
usleep(10);
//printf("%s", array_buffer[row]);
//may be executing another function call from here .... ?
free(array_buffer[row]);
}
return 1;
}
void line(char *line_char)
{
int lent = strlen(line_char);
array_buffer[count] = malloc((lent + 1)*sizeof(char));
strcpy(array_buffer[count], line_char);
#pragma omp parallel
if (count == 8)
{
int returning, returned_all = 0;
returning = process(lent); //function call, waits for 10 microseconds and returns 1 as o/p
#pragma omp single
count = 0;
#pragma omp atomic
returned_all = returned_all + returning;
}
count++;
}
int main(int argc,char **argv)
{
FILE *fp = fopen(argv[1], "r");
if(fp == NULL)
{
printf("Couldn't open file %s",argv[1]);
}
char buff[512];
while (fgets(buff, 512, fp) != NULL)
{
line(buff); /*sending out an array having one line*/
}
return 0;
}
這顯然不起作用。我知道我搞砸了omp單,但omp原子似乎是正確的。任何幫助,建議或更正,讓這個工作?