c - how to parallelize a dot product with MPI -
i've been trying learn mpi , i've code snippet c should formatted mpi make parallizable;
// dot product int a[100000]; int b[100000]; int c=0; (int i=0 ; i<100000; i++){ c += a[i] * b[i]; }
im kinda confused of how deal for-loops when using mpi answer exercise;
// dot product int a[100000]; int b[100000]; int c=0; int rootid = 0; int numtasks, taskid, len, partner, message; mpi_status status; mpi_init(&argc, &argv); mpi_comm_rank(mpi_comm_world, &taskid); (int i=0 ; i<100000; i++){ c = a[i] * b[i]; if(rootid == taskid){ mpi_send(&a[100000], &b[100000], mpi_int, 1, 0, mpp_comm_world, mpi_status) }else if (rootid < taskid){ mpi_recv(c, 100000, mpi_int, 0, 0, mpp_comm_world, mpi_status) } mpi_finalize(); return 0; }
im not sure if correct, believe im in right direction...
the problem mpi can't find examples of how rewrite or structe loops - there examples in fortran nothing im familiar with... i've seen simple examples of "hello world" c in mpi.... nothing useful there.
all appreciated..
was there typo , did mean instead ?
for (int i=0 ; i<100000; i++){ c += a[i] * b[i]; }
parallelization heavily depends on how arrays a
, b
distributed.
the simplest case, 1 biggest memory footprint, have full arrays a
, b
on mpi tasks. based on task rank , total number of tasks, each task can compute part of dot product e.g.
for (int i=start; i<end; i++) { c += a[i] * b[i]; }
and can mpi_reduce()/mpi_allreduce()
mpi_sum
partial dot products in order final result.
Comments
Post a Comment