我有以下代码在Serial中完美运行:
while(fread(in, sizeof(in), 1, fp_input) && ++count){
Encode();
fseek(fp_out, (count-1)*sizeof(out), SEEK_SET);
fwrite(out, sizeof(out), 1, fp_out);
time_spent = (double)(stop - start)/CLOCKS_PER_SEC;
}
为了并行化代码,我使用以下OpenMP指令:
#pragma omp parallel private(in, out)
{
while(read(fd_read, in, sizeof(in)) && ++count){
Encode(); //take input from in and writes to out
fseek(fp_out, (count-1)*sizeof(out), SEEK_SET);
fwrite(out, sizeof(out), 1, fp_out);
time_spent = (double)(stop - start)/CLOCKS_PER_SEC;
}
}
必须保持命令作为写入输出文件,以便以后可以解密。但是我无法实现它,如何构造OpenMp指令来实现文件写入顺序?
将代码更新为以下内容,并在序列代码中获得了显着的加速:
while(fread(chunk_input, sizeof(chunk_input), 1, fp_input) == 1){
int block_no = 0;
#pragma omp parallel for private(block_no)
for(block_no=0; block_no<CHUNK_SIZE; block_no += 16){
unsigned char in[16], out[16];
memcpy(in, &chunk_input[block_no], 16);
Encode(in, out);
memcpy(&chunk_output[block_no], out, 16);
}
fwrite(chunk_output, sizeof(chunk_output), 1, fp_out);
}