MPI代码的链接错误:未定义的引用`ceil'

时间:2015-10-17 04:58:39

标签: c mpi

在尝试编译我的MPI矩阵乘法代码时,我收到以下错误:

undefined reference to `ceil'

错误细节也在下面描述。谢谢 我该如何解决这个问题?

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

#define Arow 3
#define Acol 2 
#define Max_Value 10
//process maping function

int proc_map(int i, int size) {
    size = size-1;
    int r = (int) ceil((double)Arow / (double)size);
    int proc = i/r;
    return proc + 1;
}


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

    if (rank == 0) {

        int a[Arow][Acol];
        int b[Acol];
        int c[Arow];

        /*Generating Random Values for A & B Array */

        srand (time (NULL));
        int i;
        for (i=0; i<Arow; i++) {
            int j;
            for (j=0; j<Acol; j++) {
                if (i==0) b[j]= rand() % Max_Value;
                a[i][j]= rand() % Max_Value;
            }
        }
        /*Printing the Matrix*/

        printf("Matrix A :\n");
        for (i=0; i<Arow; i++) {
            int j;
            for (j=0; j<Acol; j++) {
                printf ("%3d ", a[i][j]);
            }
            printf("\n");
        }
        printf("\nMatrix B : \n");
        for (i=0; i<Acol; i++) {
            printf ("%3d ", b[i]);
        }
        printf ("\n\n");

        // Sending B Values to other processes
        int j;
        for (j=1; j<size; j++) {
            MPI_Send(b, Acol, MPI_INT, j, 99, MPI_COMM_WORLD);
        }
        // Sending Required A Values to specific process
        for (i=0; i<Arow; i++) {
            int processor = proc_map(i, size);
            MPI_Send(a[i], Acol, MPI_INT, processor, (100*(i+1)), MPI_COMM_WORLD);
        }
        // Gathering the results from other processes
        for(i=0; i<Arow; i++) {
            int source_process = proc_map(i, size);
            MPI_Recv(&c[i], 1, MPI_INT, source_process, i, MPI_COMM_WORLD, &Stat);
            printf("P%d : c[%d]\t= %d\n", rank, i, c[i]);
        }
    }
    else {
        int b[Acol];
        // Each process get B Values from  MAster
        MPI_Recv(b, Acol, MPI_INT, 0, 99, MPI_COMM_WORLD, &Stat);
        //Get Required A Values from Master then Compute the result
        int i;
        for (i=0; i<Arow; i++) {
            int processor = proc_map (i, size);
            if (rank == processor) {
                int buffer[Acol];
                MPI_Recv(buffer, Acol, MPI_INT, 0, (100*(i+1)), MPI_COMM_WORLD, &Stat);
                int sum = 0;
                int j;
                for (j=0; j<Acol; j++) {
                    sum = sum + (buffer[j]*b[j] );
                }
                MPI_Send(&sum, 1, MPI_INT, 0, i, MPI_COMM_WORLD);
            }
        }
    }
    MPI_Finalize();
    return 0;
}

以下是我得到的确切错误:

    $ mpicc matrix.c -lm
    $ mpicc -o matrix matrix.c
    /usr/bin/ld: /tmp/ccjAhKlE.o: undefined reference to symbol 'ceil@@GLIBC_2.2.5'
    /usr/bin/ld: note: 'ceil@@GLIBC_2.2.5' is defined in DSO /lib64/libm.so.6 so try adding it to the linker command line
    /lib64/libm.so.6: could not read symbols: Invalid operation
    collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:2)

您的问题与MPI无关。实际上,我甚至没有尝试阅读te代码本身。我只是试图编译它:

$ mpicc mat.c 
/tmp/ccc0MvN2.o: In function `proc_map':
mat.c:15: undefined reference to `ceil'
mat.c:15: undefined reference to `ceil'
mat.c:15: undefined reference to `ceil'
mat.c:15: undefined reference to `ceil'
collect2: error: ld returned 1 exit status

现在,这是链接时缺少的数学库的明确内容。因此,添加-lm会解决问题。

$ mpicc mat.c -lm
$ #no error, compilation and link just worked