1

I actually have two questions but it seems they may be connected:

1) I've tried to run basic MPI example:

#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    int rank, size;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    printf("I am %d from %d\n", rank, size);
    MPI_Finalize();
    return 0;
}

It has to output something like:

I am 0 from 2
I am 1 from 2

Although I'm getting the following:

$ mpicc mpi_hello.c -o hello    
$ mpirun -np 4 ./hello          
I am 0 from 1
I am 0 from 1
I am 0 from 1
I am 0 from 1
$ mpirun -np 2 ./hello
I am 0 from 1
I am 0 from 1

Is it somehow connected to thread definition in Linux? I'm running it on Ubuntu 16.04.

2) My OpenMP program:

#include <omp.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <stdio.h>

const int N = 10000;

int matrix[N][N];

int main()
{
    #pragma omp parallel num_threads(2)

    #pragma omp for
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            matrix[i][j] = 1+i;

    clock_t t;

    t = clock();

    #pragma omp parallel num_threads(2)
    #pragma omp for
    for (int i = 0; i < N; i++)
    {
        matrix[i][i] = 0;
        for (int j = 0; j< N; j++)
            if (j != i)
                matrix[i][i] += sin(cos(log(matrix[i][j] + matrix[j][i])));
    }
    t = clock() - t;

    std::cout << "It took " << ((float)t)/CLOCKS_PER_SEC << " sec" << std::endl;

    return 0;
}

It works correctly and uses 2 threads. However, it loads 2 processors (~100% CPU) and takes the same time (~34 seconds) as the similiar consequtive one (loads 1 processor ~50% CPU). I know that OpenMP may need some time to start, but how can it result in the same duration of programs?

2
  • 1
    Do not use clock to measure parallel run time. It has been treated here perhaps thousand times... Commented May 30, 2017 at 20:30
  • 1
    These are two different questions and should be asked separately. The OpenMP part is clearly a duplicate of many other questions here. Commented May 30, 2017 at 20:32

1 Answer 1

1

Answering to the MPI part of the question. Do you have MPICH installed? If is that so, try to compile and run like this:

$ mpicc.mpich mpi_hello.c -o hello    
$ mpirun.mpich -np 4 ./hello          
I am 0 from 4
I am 1 from 4
I am 2 from 4
I am 3 from 4

It should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.