locked
problem on mpi program RRS feed

  • Question

  • İ have some codes with mpi (mipch) on c++ (visiul studio 2008). but have problems. please help me.

    codes is here:

    #include "stdafx.h"
    #include "VecLib.h"
    #include "stdio.h"
    #include "mpi.h"

    #define N 5



    // C = ( A * B)
    // MD = MA * MB
    // E = (MC * A) + (MB * B)


    void F1(void)
    {

    // C = ( A * B)

    printf("Task 1 has started\n");
    int A[N];
    int B[N];
    int C=0;
    int i;

    for(i=0; i<N; i++){
    A=1;
    B=1;
    }


    for(i=0; i<N; i++){
    C = (A*B)+C;
    }

    printf("C= %d\n", C);

    printf("Task 1 has finished ") ;
    printf("\n");
    printf("\n");


    }


    //---------------------------------------------------------------
    void F2(void){

    // MD = MA * MB

    printf("Task 2 has started\n");

    int MD[N][N];
    int MA[N][N];
    int MB[N][N];




    for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
    MA[j]=1;
    MB[j]=1;

    }
    }


    for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
    MD[j]=0;
    for (int k = 0; k < N; k++){

    MD[j]= MA[j] * MB[k][j] + MD[j];

    }
    }
    }


    printf("MD= \n");


    for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
    printf("%d %d", MD[j]);
    }
    printf("\n");
    }

    printf("Task2 has finished\n");
    printf("\n");
    printf("\n");



    }

    //---------------------------------------------------------------
    void F3(void)
    {
    // E = (MC * A) + (MB * B)

    printf("Task3 has started\n");

    int MC[N][N];
    int MB[N][N];
    int E[N];
    int A[N];
    int B[N];
    int S[N];
    int Z[N];


    for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++){
    E=0;
    A=1;
    B=1;
    MC[j]=1;
    MB[j]=1;
    }
    }


    for (int i = 0; i < N; i++) {
    S=0;
    Z=0;
    for (int j = 0; j < N; j++) {
    S=(MC[j]*A[j])+S;
    Z=(MB[j]*B[j])+Z;
    }
    }



    for(int i = 0; i < N; i++){
    E=S+Z;
    }


    printf("E= \n");

    for(int i = 0; i < N; i++){
    printf("%d ", E);
    }


    printf("\n");

    printf("Task3 has finished\n");
    printf("\n");
    printf("\n");

    }


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

    MPI_Status sts;
    int threads, numThread;

    printf("%s\n","MainTask has started");
    printf("----------------------------");
    printf("\n");
    printf("\n");
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &threads);
    MPI_Comm_rank(MPI_COMM_WORLD, &numThread);

    if (numThread == 0){
    F1();
    }
    if (numThread == 1){
    F2();
    }
    if (numThread == 2){
    F3();
    }

    printf("----------------------------\n");
    printf("%s\n","MainTask has finished");

    char n;
    scanf_s("%c",&n);
    MPI_Finalize();
    return 0;
    }


    But when i compile this and run only F1 function is running. i think some problems on main function.
    But if i do like this:
    if (numThread == 0){
    F1();
    }
    if (numThread == 0){
    F2();
    }
    if (numThread == 0){
    F3();
    }



    its working but i know this is not paralel programing.
    necessary to write 0-1-2


    please helpl me guys.
    Wednesday, December 17, 2008 6:53 PM

Answers

  • you need to run your program using mpiexec (e.g., mpiexec -n 3 myprog.exe) to run 3 processes each with its individual rank.

    thanks,
    .Erez
    Monday, December 22, 2008 9:14 AM

All replies

  • you need to run your program using mpiexec (e.g., mpiexec -n 3 myprog.exe) to run 3 processes each with its individual rank.

    thanks,
    .Erez
    Monday, December 22, 2008 9:14 AM
  • Thank very much...
    Monday, December 22, 2008 11:47 AM