簡體   English   中英

MPI代碼的鏈接錯誤:未定義對“ ceil”的引用

[英]Link error of a MPI code: undefined reference to `ceil'

嘗試編譯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

您的問題與MPI無關。 實際上,我什至沒有嘗試自己讀取代碼。 我只是嘗試編譯它:

$ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM