MPI错误与我的代码

时间:2016-10-13 21:56:59

标签: c mpi bucket-sort

这是使用MPI进行存储桶排序的c编程。我试图让这个代码运行,但有MPI错误。似乎错误是来自函数Put_numbers_in_bucket的最后一部分与MPI_send和MPI_recv?

代码是:

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

#define SIZE 16                 

void Put_numbers_in_bucket(long int[], long int[], int, int, int, int);
void Sequential_sort(long int[], int);
int  Get_minpos(long int[], int);

int main(int argc, char** argv)
{
long int * big_array;
long int * local_array;
int i, j, k;
int n;
int master = 0;
int my_id;
int numprocs;
int bucketsize;

//MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
bucketsize = SIZE / numprocs;

if (my_id == master)
{
    printf("The number of processes available is %d.\n", numprocs);
    printf("The random generated numbers are: \n");
    big_array = malloc(SIZE*sizeof(long int));
    for (i = 0; i < SIZE; i++){
        big_array[i] = rand() % 1200;
        //printf("%2d  ", big_array[i]);
        printf("%7ld %c", big_array[i], i % 8 == 7 ? '\n' : ' ');
    }
    printf("\n");
}

//MPI_Bcast(big_array, SIZE, MPI_LONG, 0, MPI_COMM_WORLD);
n = SIZE;
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
local_array = malloc(bucketsize*sizeof(long int));

Put_numbers_in_bucket(big_array, local_array, SIZE, bucketsize, numprocs, my_id);
Sequential_sort(local_array, bucketsize);

MPI_Gather(local_array, bucketsize, MPI_LONG, big_array, bucketsize, MPI_LONG, 0, MPI_COMM_WORLD);

if (my_id == master)
{
    printf("\n");
    printf("Sorted Result - Master process:\n");
    for (i = 0; i < SIZE; i++) {
        printf("%7ld %c", big_array[i], i % 8 == 7 ? '\n' : ' ');
    }
}
    free(local_array);
    free(big_array);
MPI_Finalize();
}

void Sequential_sort(long int array[], int  size)
{
int      eff_size, minpos;
long int temp;

for (eff_size = size; eff_size > 1; eff_size--)
{
    minpos = Get_minpos(array, eff_size);
    temp = array[minpos];
    array[minpos] = array[eff_size - 1];
    array[eff_size - 1] = temp;
}
}
    int Get_minpos(long int array[], int eff_size)
    {
        int i, minpos = 0;

        for (i = 0; i<eff_size; i++)
            minpos = array[i] > array[minpos] ? i : minpos;
        return minpos;
    }

void Put_numbers_in_bucket(long int big_array[], long int local_array[], int size, int bucketsize, int numprocs, int my_id)
{
int i, bucket;
MPI_Status status;

if (my_id == 0) {
    size = SIZE;
    for (i = 0; i< size; i++) {
        bucket = big_array[i] / (2 * bucketsize);  /* Assume the range is 2*n */
        MPI_Send(&big_array[i], 1, MPI_LONG, bucket, 0, MPI_COMM_WORLD);            
    }
}
for (i = 0; i<bucketsize; i++) {
    MPI_Recv(&local_array[i], 1, MPI_LONG, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
}
}

错误消息是:

[cluster1B.CDSE.:31007] *** An error occurred in MPI_Send
[cluster1B.CDSE.:31007] *** on communicator MPI_COMM_WORLD
[cluster1B.CDSE.:31007] *** MPI_ERR_RANK: invalid rank
[cluster1B.CDSE.:31007] *** MPI_ERRORS_ARE_FATAL: your MPI job will now abort
The number of processes available is 4.
The random generated numbers are:
583      886      777      115      593     1135      586      492
249     1021      362      427      290      859      563     1126

--------------------------------------------------------------------------
mpirun has exited due to process rank 0 with PID 31007 on
node cluster1B.CDSE exiting improperly. There are two reasons this could occur:

1. this process did not call "init" before exiting, but others in
the job did. This can cause a job to hang indefinitely while it waits
for all processes to call "init". By rule, if one process calls "init",
then ALL processes must call "init" prior to termination.

2. this process called "init", but exited without calling "finalize".
By rule, all processes that call "init" MUST call "finalize" prior to
exiting or it will be considered an "abnormal termination"

This may have caused other processes in the application to be
terminated by signals sent by mpirun (as reported here).
--------------------------------------------------------------------------

0 个答案:

没有答案