#define宏定义问题
我编的是C语言的MPI并行程序,实现矩阵相乘。但是用gdb调试得时候发现,宏定义得参数都是对的
但是宏定义HIGH的值就是不对。下面是一部分程序,求大侠帮忙
#include<stdio.h>
#include<stdlib.h>
#include<mpi.h>
#define INPUT_DATA 1
#define LOW(id,p,n) ((id*n)/p)
#define HIGH(id,p,n) (LOW(id+1,p,n)-1)
#define SIZE(id,p,n) (HIGH(id,p,n)-LOW(id,p,n)+1)
void main(int argc,char *argv[]){
int **set,*stone;/*the left matrix*/
int *vector;/*The right matrix*/
int m,n;/*row and col*/
int id,p,control=1;/*MPI paraments*/
MPI_Status *status;
double time;/*test estimate*/
int local_row;/*the local row*/
int i,j;/*The temp ptr*/
int *net_size,*net_begin;
int *local_answer,*net_answer;
MPI_Init(&argc,&argv);
MPI_Barrier(MPI_COMM_WORLD);
time=-MPI_Wtime();
MPI_Comm_rank(MPI_COMM_WORLD,&id);
MPI_Comm_size(MPI_COMM_WORLD,&p);
/*get the row and col ,then build the area*/
if(id==0){
printf("Process %d done:input the row and col:\n",id);
scanf("%d %d",&m,&n);}
MPI_Bcast(&m,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
printf("Process %d done:row range %d ~ %d:\n",id,LOW(id,p,m),HIGH(id,p,m));
local_row=SIZE(id,p,m);
[解决办法]
手工进行宏展开看看,这个没什么复杂的。
[解决办法]
#define HIGH(id,p,n) (LOW((id+1),p,n)-1)
[解决办法]
id+1要加括号
[解决办法]
加括号是为了处理表达式参数(即宏的参数可能是个算法表达式)时不出错,因为宏替换就是文本替换,所以如果有以下情况:
#define COM(A,B) (A)*(B)
那么COM(6+5,3)这个调用会怎么替换呢?它会换成这样:
(6+5)*(3)
显然这是和COM宏的意图一致的,但是如是去掉了定义中括号,即写成这样:
#define COM(A,B) A*B
那么COM(6+5,3)这个调用会怎么替换呢?它就会换成这样:
6+5*3
这样显然就和宏的意图不符合了。