2016-09-25 41 views
0

我需要将数百个jpg图像转换为dicom。我有一个Web应用程序,其中最初由Node.js制作,但速度很慢。我想在C++做的,用的OpenMP并行下面的代码:如何使用openmp转换jpg2dicom?

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

如果您已经在使用外部命令,请考虑使用[GNU Parallel](https://www.gnu.org/software/parallel/)或['xargs -P'](https:/ /linux.die.net/man/1/xargs),这并不要求你编码那么多 – dvhh

+0

谢谢,dvhh!我会看一看! :) – Thales

回答

1

猛砸解决方案

首先,如果你认为现有的工具,你的任务会更容易些:

例与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 

的OpenMP

(关于如何做到这一点 Stackoverflow code

更容易与for循环工作,你可以开始把文件列表到一个数组。

// 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; 
}