我需要将数百个jpg图像转换为dicom。我有一个Web应用程序,其中这个部分最初是用Node.js制作的,但速度非常慢。我想在C中做,并使用openmp来并行化以下代码:
int main(){
DIR *dir;
struct dirent *arq;
dir = opendir("./jpg");
char cmd1[255] = "./dcm4che-2.0.23/bin/jpg2dcm -c dcm4che-2.0.23/etc/jpg2dcm/jpg2dcm.cfg jpg/";
char cmd_aux[255] = "./dcm4che-2.0.23/bin/jpg2dcm -c dcm4che-2.0.23/etc/jpg2dcm/jpg2dcm.cfg jpg/";
char buf[255];
char nomeArq[255];
int i;
//Stretch in which I use openmp
while ((arq = readdir(dir)) != NULL){
strncpy(nomeArq, arq->d_name, 255);
if(nomeArq[0] != '.'){
sprintf(buf, " dcm/imagem-%d.dcm", i);
strcat(cmd1, nomeArq); // cmd1 + nomeArquivo
strcat(cmd1, buf);
system(cmd1);
strncpy(cmd1, cmd_aux, 255);
}
i++;
}
closedir(dir);
return 0;
}
我怎么知道这个代码是I / O绑定的,我想问一下openmp是否真的无法获得任何加速。如果可能的话,如何在使用openmp时并行化这个循环。如果我不是很清楚,对不起!我还在学习英语!
答案 0 :(得分:1)
首先,如果考虑现有工具,您的任务会更容易:
xargs
xargs
示例(来自bash命令提示符):
ls ./jpg | xargs -P 0 -i ./dcm4che-2.0.23/bin/jpg2dcm -c dcm4che-2.0.23/etc/jpg2dcm/jpg2dcm.cfg jpg/{} dcm/{}.dcm
使用for
循环更容易,您可以开始将文件列表放入数组(Stackoverflow code如何操作)。
// put your code for reading he files list into an array //
int main() {
char **files;
const size_t count = file_list("./jpg", &files);
#pragma omp parallel for
for(size_t i=0;i<count;++i) {
if(files[i][0] == '.' ) { continue; } // 'skip' directory entry, maybe you should implement some better check ( extension, or even checking if it is a file at all)
// keep the buffer local to the loop
char *buf = malloc(1024);
// already calling sprintf, use it for building the whole command line, and avoid the strncpy & strcat
sprintf(buf, "./dcm4che-2.0.23/bin/jpg2dcm -c dcm4che-2.0.23/etc/jpg2dcm/jpg2dcm.cfg jpg/%s dcm/imagem-%zu.dcm",files[i],i);
system(buf);
free(buf); // cleanup
}
return EXIT_SUCCESS;
}