【发布时间】:2016-02-21 01:12:39
【问题描述】:
我想创建一个数组(称为 Csend),然后创建一个稍微修改它的函数,例如为每个元素添加 0.05。我遇到的问题是数组的格式,我不确定如何正确地将它传递给函数。我在this guide 之后以这种方式分配了内存,以便以后可以将其放入 MPI_Send 和 MPI_Recv。
这是我的尝试:
#include "stdio.h"
#include "stdlib.h"
#include "mpi.h"
#include "math.h"
int main(int argc, char **argv) {
int N = 32;
int dim = 3;
float a = 10.0; // size of 3D box
int size, rank, i, j, k, q;
float **C, **Csend, **Crecv;
float stepsize = 0.05;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
float **alloc_2d_float(int rows, int cols) {
float *data = (float *)malloc(N*dim*sizeof(float));
float **array= (float **)malloc(N*sizeof(float*));
for(i=0; i<N; i++) {
array[i] = &(data[dim*i]);
}
return array;
}
C = alloc_2d_float(N,dim);
Csend = alloc_2d_float(N,dim);
Crecv = alloc_2d_float(N,dim);
if(rank == 0) {
for (i = 0; i < N; i++) {
for (j = 0; j < dim; j++) {
Csend[i][j] = (float)rand()/(float)(RAND_MAX/a);
}}
}
// FUNCTION TO MODIFY MATRIX //
float randomsteps(float *matrix, int N, int dim) {
int i, j;
for(i = 0; i < N; i = i+2) {
for (j = 0; j < dim; j++) {
*((matrix+i*N) + j) = *((matrix+i*N) + j) + stepsize;
}
}
return matrix;
}
C = randomsteps(Csend, 32, 3);
for (i=0; i<N; i++){
for (j=0; j<dim; j++){
printf("%f, %f\n", Csend[i][j], C[i][j]);
}
}
MPI_Finalize();
return 0;
}
我遇到的问题是像这里一样格式化,我收到错误消息,并且以没有给出错误消息的方式格式化,C 只是空的。
这是错误信息:
test.c: In function ‘randomsteps’:
test.c:46: error: incompatible types when returning type ‘float *’ but ‘float’ was expected
test.c: In function ‘main’:
test.c:49: warning: passing argument 1 of ‘randomsteps’ from incompatible pointer type
test.c:39: note: expected ‘float *’ but argument is of type ‘float **’
test.c:49: error: incompatible types when assigning to type ‘float **’ from type ‘float’
感谢您的帮助!
【问题讨论】:
-
能发Minimal, Complete, and Verifiable example就更好了。通过查看发布的代码很难理解您的程序在做什么。
-
嗯,有什么问题吗? (即错误消息是什么?)
-
对不起,我已经编辑了它,所以有更多的程序。在这种情况下,错误是“返回类型'float *'但预期'float'时的类型不兼容”这似乎是一个小问题,但每次我尝试修复一件事时,它似乎都会演变成另外三个问题。
-
错误出现在哪里?