OpenMP在for循环中并行化代码块?

时间:2010-09-27 04:29:16

标签: c++ openmp

问候所有人,

我想在单独的OpenMP线程中运行循环内的代码块。 我是否在以下代码片段中定义了正确的OpenMP指令:

#ifdef OPENMP_ENABLE
        #pragma omp parallel for
    #endif

for(int i=0;i<numOfSlices;i++){                  // Entire block inside this loop should be fun in new OpenMP thread

        int id =0;

        #ifdef OPENMP_ENABLE
               id=omp_get_thread_num();
              qDebug("------- OPENMP thread number  %d",id);
        #endif
            qDebug("      - Image Slice %d",i);

        int width=0,height=0;
        //Image Buffer
        unsigned char *buff=planeViewer->getImageBuggerOfSlice(i,height,width);

        //Trunk
        RzTrunk *rztrnk=planeViewer->getTrunkOfSlice(i);

        //If Empty trunk,add frame @TODO Hard coded
        if(rztrnk->getCurveCount()==0){
           RzCurve *crv=rztrnk->createFrameCurve(10,10,width-10,height-10);
           rztrnk->addCurve(crv);
        }

       //Covert Rinzo Trunk to Centy Trunk!
       //TODO Make sure to deallocate memory
       QImage *qimg=new QImage(buff,width,height,QImage::Format_RGB32);
       trnk_t *trnkt = convertRzTrunkToCntyTrunk(rztrnk, qimg->width(),qimg->height());


       //Convert QImage to Cnty Image format
       image_t *imgt=convertQImageToImaget(qimg);

       //This extract parts , first should detect outer border
       if(etype==EXTRACT_DEFL_INOUT){
            cnty_extract_contour(EXTRACT_DEFL_OUT, trnkt, imgt, NULL, NULL,settings2, NULL);
            trnk_make_crvs_nodes_active(trnkt);
       }

       cnty_extract_contour(etype, trnkt, imgt, NULL, NULL,settings, NULL);
       convertCntyTrunkToRzTrunk(trnkt, rztrnk);

       trnk_destruct(trnkt);
       destroyImaget(imgt);
       delete qimg;
       delete buff;    
} //end of for loop

1 个答案:

答案 0 :(得分:0)

我发现了问题。原因是“cnty_extract_contour()”方法不是线程安全的。