0

我有一个巨大的顺序程序,我想在其中将一些算法与 MPI 和 CUDA 并行化。如何正确地将顺序部分与并行部分分开?问题在于并行算法的嵌套,以及 slurm 或 loadLeveler 的使用(例如,在我的 MPI 集群上,我不能写类似的东西:) mpirun -n 1 a.out: -n 2 b.out

例子:

int main()
{
  funcA();
}
void funcA() 
{
  funcB();
}

void funcB()
{
  parallel algo starts here....
}
4

1 回答 1

1

对于这个问题,我找到了一个很好的解决方案。这是示例代码:

 #include <iostream>
 #include <mpi.h>
 #include <unistd.h>

 using namespace std;

 int main(int argc, char** argv) {

MPI_Init(&argc, &argv);
int r;
MPI_Comm_rank(MPI_COMM_WORLD, &r);

if (r == 0) {
    cout << "[GUI]Start perfoming initialization...." << endl;
    sleep(2);
    cout << "[GUI]Send command to start execution...." << endl;
    int command = 1;
    //TODO: now it's hardcoded to send data to 1 proc
    MPI_Send(&command, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
    cout << "[GUI]Waiting for execution results..." << endl;
    int buf[5];
    MPI_Recv(&buf, 5, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    for (int i=0; i<5; i++)
    {
        cout << "buf["<< i << "] = " << buf[i] << endl;
    }
} else {
    int command;
    MPI_Recv(&command, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    cout << "Received command: " << command << endl;
    if (command == 1) {
        cout << "[ALGO]Receive command to start execution" << endl;
        sleep(2);
        cout << "[ALGO]Send computed data..." << endl;
        int buf[5] = {5,4,3,2,1};
        MPI_Send(&buf, 5, MPI_INT, 0, 0, MPI_COMM_WORLD);
    }
}


MPI_Finalize();
return 0;
  }
于 2013-02-23T20:21:57.007 回答