矩阵—向量乘法的并行计算怎么解决?请教了~~

pickiet 2007-04-19 10:57:34
在科技大学的网站上看到这道题 但是不会做理解不了请教各位高手这个怎么写?
write a parallel matrix multiplication program: C = A * B. The program should be able to accept any size square matrix and utilize any number of processors

翻译过来就是 写一个并行矩阵乘法程序。让矩阵C=矩阵A*矩阵B。这个程序必须能够接受任何大小的方矩阵和利用任何数量的处理器。

我看了些资料可以利用Master处理器收集工作处理器的计算结果并且存储到矩阵C里面 但是一点头绪都没有 请教各位大大这个程序应该怎么写?
...全文
864 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ning_yang 2007-06-05
  • 打赏
  • 举报
回复
上面写的那个程序是一个A(4,4)*B(4,1)=C(4,1)的例子。使用并行处理技术可以提高计算的效率,你如果想了解更多的相关资料可以在google上搜都志辉或者迟学斌,这两个人都有关于并行计算的出版材料!
ning_yang 2007-06-05
  • 打赏
  • 举报
回复
include <stdio.h>
#include <mpi.h>
#define SIZE 4
int main(int argc, char **argv) {
int j;
int rank, size, root;
float X[SIZE];
float X1[SIZE];
float Y1[SIZE];
float Y[SIZE][SIZE];
float Z[SIZE];
float z;
root = 0;
/* Initialize MPI. */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
/* Initialize X and Y on root node. Note the row/col alignment. This is specific to C */
if (rank == root) {
Y[0][0] = 1; Y[1][0] = 2; Y[2][0] = 3; Y[3][0] = 4;
Y[0][1] = 5; Y[1][1] = 6; Y[2][1] = 7; Y[3][1] = 8;
Y[0][2] = 9; Y[1][2] = 10;Y[2][2] = 11;Y[3][2] = 12;
Y[0][3] = 13;Y[1][3] = 14;Y[2][3] = 15;Y[3][3] = 16;
Z[0] = 1;
Z[1] = 2;
Z[2] = 3;
Z[3] = 4;
}
MPI_Barrier(MPI_COMM_WORLD);
/* root scatters matrix Y in 'SIZE' parts to all nodes as the matrix Y1 */
MPI_Scatter(Y,SIZE,MPI_FLOAT,Y1,SIZE,MPI_FLOAT,root,MPI_COMM_WORLD);
/* Z is also scattered to all other nodes from root. Since one element is sent to
all nodes, a scalar variable z is used. */
MPI_Scatter(Z,1,MPI_FLOAT,&z,1,MPI_FLOAT, root,MPI_COMM_WORLD);
/* This step is carried out on all nodes in parallel.*/
for(j=0;j<SIZE;j++){
X1[j] = z*Y1[j];
}
/* Now rows are added, using MPI_SUM (using recursive halving and doubling algorithm,
internal to the MPI implementation) */
MPI_Reduce(X1,X,SIZE,MPI_FLOAT,MPI_SUM, root,MPI_COMM_WORLD);
if (rank == 0) {
printf("%g\n",X[0]);printf("%g\n",X[1]);printf("%g\n",X[2]);printf("%g\n",X[3]);
}
MPI_Finalize();
return 0;
}
ReverseEngineering 2007-05-29
  • 打赏
  • 举报
回复
多处理器?
猪的飞想 2007-04-19
  • 打赏
  • 举报
回复
他指的并行处理是什么意思啊?
pickiet 2007-04-19
  • 打赏
  • 举报
回复
有人能帮忙吗
实在不会做了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧