使用MPI在数组中查找元素?

时间:2015-06-16 15:13:13

标签: c++ mpi

问题是在数组中找到一个元素,通知其他处理器停止。 因此,如果进程找到了元素,则其他人必须结束搜索。

.......
.......
localData = new int[sendcounts[rank]];
    MPI_Scatterv(data, sendcounts, displs, MPI_INT,localData,sendcounts[rank], MPI_INT, 0, MPI_COMM_WORLD);

    //FIND ELEMENT KEYNUMBER IN LOCALDATA
    for(int i=0;i<sendcounts[rank];i++) {
        cout<<"Index: "<<i<<endl;
        if(localData[i] == keyNumber){
            found = 1;
            //INVIA TROVATO
            for(int j=0;j<numprocs;j++){
                if(j!=rank){
                    MPI_Isend(&found,1,MPI_INT,j,10,MPI_COMM_WORLD,&request);
                    cout<<"inviato"<<endl;
                }
            }
            MPI_Wait(&request,&status);
            break;
        }
        MPI_Irecv(&foundReceived,1,MPI_INT,MPI_ANY_SOURCE,10,MPI_COMM_WORLD,&request);
        cout<<"Valore Found: "<<foundReceived<<endl;
        if(foundReceived == 1){
            cout<<"si";
            break;
        }
    }
    ......
    ....
    ..
    .

0 个答案:

没有答案