gpt4 book ai didi

c - MPI代码链接错误: undefined reference to `ceil'

转载 作者:行者123 更新时间:2023-11-30 18:53:16 26 4
gpt4 key购买 nike

在尝试编译我的 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

关于c - MPI代码链接错误: undefined reference to `ceil' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33182908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com