diff options
Diffstat (limited to 'modules/mpi/examples')
-rwxr-xr-x | modules/mpi/examples/MPIHelloWorld.sci | 38 | ||||
-rwxr-xr-x | modules/mpi/examples/MPIHelloWorldDouble.sci | 38 | ||||
-rwxr-xr-x | modules/mpi/examples/MPIPi.sci | 106 | ||||
-rwxr-xr-x | modules/mpi/examples/MPIPi_seq.sci | 68 | ||||
-rwxr-xr-x | modules/mpi/examples/foo.sci | 15 | ||||
-rwxr-xr-x | modules/mpi/examples/simple.sci | 9 |
6 files changed, 274 insertions, 0 deletions
diff --git a/modules/mpi/examples/MPIHelloWorld.sci b/modules/mpi/examples/MPIHelloWorld.sci new file mode 100755 index 000000000..04af78d41 --- /dev/null +++ b/modules/mpi/examples/MPIHelloWorld.sci @@ -0,0 +1,38 @@ +function MPIHelloWorld() + //////////// + // PARALLEL / initialization, include MPI_Init time in measurement + //////////// + MPI_Init(); // should have lambooted first + rnk = MPI_Comm_rank(); // let it abort if it fails + sizeNodes = MPI_Comm_size(); + + SLV = rnk; // handy shortcuts, master is rank 0 + Master = ~ SLV; // slaves are all other + + if Master + + disp("We have "+string(sizeNodes) + " processors") + for slaveId = 1:sizeNodes-1 + MPI_Send("== FROM Master == MPI_Send",slaveId) + end + + for slaveId = 1:sizeNodes-1 + tag=0 + valueBack=MPI_Recv(slaveId, tag) + disp("<=> VALUE BACK <=> "+valueBack+ " from " +string(slaveId)) + + end + else + // slave + disp("Processor "+string(rnk)) + rankSource=0 + tag=0 + value=MPI_Recv(rankSource, tag) + value="(Modified by the slave) "+value + MPI_Send(value,0) + + end + + MPI_Finalize() + +endfunction diff --git a/modules/mpi/examples/MPIHelloWorldDouble.sci b/modules/mpi/examples/MPIHelloWorldDouble.sci new file mode 100755 index 000000000..3cac2c0f8 --- /dev/null +++ b/modules/mpi/examples/MPIHelloWorldDouble.sci @@ -0,0 +1,38 @@ +function MPIHelloWorldDouble() + //////////// + // PARALLEL / initialization, include MPI_Init time in measurement + //////////// + MPI_Init(); // should have lambooted first + rnk = MPI_Comm_rank(); // let it abort if it fails + sizeNodes = MPI_Comm_size(); + + SLV = rnk; // handy shortcuts, master is rank 0 + Master = ~ SLV; // slaves are all other + + if Master + + disp("We have "+string(sizeNodes) + " processors") + for slaveId = 1:sizeNodes-1 + MPI_Send([23,42;68,62],slaveId) + end + + for slaveId = 1:sizeNodes-1 + tag=0 + valueBack=MPI_Recv(slaveId, tag) + disp("<=> VALUE BACK <=> "+valueBack+ " from " +string(slaveId)) + + end + else + // slave + disp("Processor "+string(rnk)) + rankSource=0 + tag=0 + value=MPI_Recv(rankSource, tag) + value="(Modified by the slave) "+value + MPI_Send(value,0) + + end + + MPI_Finalize() + +endfunction diff --git a/modules/mpi/examples/MPIPi.sci b/modules/mpi/examples/MPIPi.sci new file mode 100755 index 000000000..14b5ebe7b --- /dev/null +++ b/modules/mpi/examples/MPIPi.sci @@ -0,0 +1,106 @@ +function MPIPi(N,mod) + // Pi_seq: Compute PI (sequential) by num.integr. of arctan(x) in [0..1] + // + // Pi_seq [ (N) ] + // + // N [1E7] #subdivisions of the [0, 1] interval + // mod ['s'] communication modality: (s)end (r)educe + // printed results struct contains + // pi estimated pi value + // err error + // time from argument xmit to pi computed + // + // Examples: + // + // octave:1> Pi_seq + // results = + // { + // pi = 3.1416 + // err = -6.4837e-14 + // time = 2.7761 + // } + // + + //////////////////// + // ArgChk // + //////////////////// + if argn(2)<1, N=1E7; end + if argn(2)<2, mod="s"; end + if argn(2)>2, error("usage MPIPi(N,mod)"); end + flag=0; + [%H,%ierr] = evstr(string(N)); + flag=flag | size(N,"*")<>1 | %ierr<>0; + flag=flag | fix(N)~=N | N<1; + if flag, error("usage MPIPi( <int> N>0, <char> mod==''s|r'' )"), end + + //////////////////////////////////// + // Results struct // + //////////////////////////////////// + results.pi =0; + results.err =0; + results.time=0; + + //////////// + // PARALLEL / initialization, include MPI_Init time in measurement + //////////// + // T=clock; / + //////////// + MPI_Init(); // should have lambooted first + rnk = MPI_Comm_rank(); // let it abort if it fails + siz = MPI_Comm_size(); + + SLV = rnk; // handy shortcuts, master is rank 0 + MST = ~ SLV; // slaves are all other + + if MST + disp("Master here") + else + disp("Slave here") + end + + //////////// + // PARALLEL / computation (depends on rank/size) + //////////// // vectorized code, equivalent to + width=1/N; lsum=0; // for i=rnk:siz:N-1 + i=rnk:siz:N-1; // x=(i+0.5)*width; + x=(i+0.5)*width; // lsum=lsum+4/(1+x^2); + lsum=sum(4 ./(1+x.^2)); // end + // mem-bound, N=1E7 => size(i)=8E7/siz (80MB!!!) + //////////// + // PARALLEL / reduction and finish + //////////// + select mod + case "s", TAG=7; // Any tag would do + if SLV // All slaves send result back + MPI_Send(lsum, 0,TAG); + else // Here at master + Sum =lsum; // save local result + for slv=1:siz-1 // collect in any order + // MPI_Recv(lsum,MPI_ANY_SOURCE,TAG,MPI_COMM_WORLD); + MPI_Recv(lsum,TAG); + Sum = Sum + lsum; // and accumulate + end // order: slv or MPI_ANY_SOURCE + end + case "r", Sum=0; // reduction master = rank 0 @ WORLD + MPI_Reduce(lsum,Sum, MPI_SUM, 0,MPI_COMM_WORLD); + end + + MPI_Finalize; + + ////////////////////////////////////////////////////////////////// + //results.time = etime(clock,T); // + ////////////////////////////////////////////////////////////////// + results.err = Sum-%pi; + results.pi = Sum // ; + + + ////////////////////////////// + // FINAL CHECK // + ////////////////////////////// + if abs(results.err)>5E-10 + warning("pretty nice pi value! go fix it") + end + disp(results) +endfunction + + diff --git a/modules/mpi/examples/MPIPi_seq.sci b/modules/mpi/examples/MPIPi_seq.sci new file mode 100755 index 000000000..fc7216065 --- /dev/null +++ b/modules/mpi/examples/MPIPi_seq.sci @@ -0,0 +1,68 @@ +function MPIPi_seq(N) + // Pi_seq: Compute PI (sequential) by num.integr. of arctan(x) in [0..1] + // + // Pi_seq [ (N) ] + // + // N [1E7] #subdivisions of the [0, 1] interval + // + // printed results struct contains + // pi estimated pi value + // err error + // time from argument xmit to pi computed + // + // Examples: + // + // octave:1> Pi_seq + // results = + // { + // pi = 3.1416 + // err = -6.4837e-14 + // time = 2.7761 + // } + // + + //////////////////// + // ArgChk // + //////////////////// + if argn(2)<1, N=1E7; end + if argn(2)>1, error("usage Pi_seq(N)"); end + flag=0; + [%H,%ierr] = evstr(string(N)); + flag=flag | size(N,"*")<>1 | %ierr<>0; + flag=flag | fix(N)~=N | N<1; + if flag, error("usage Pi_seq( <int> N>0, <char> mod==''s|r'' )"), end + + //////////////////////////////////// + // Results struct // + //////////////////////////////////// + results.pi =0; + results.err =0; + results.time=0; + + //////////////////////////// + // SEQUENTIAL // code, vectorized + //////////////////////////// + // T=clock; // + //////////////////////////// // vectorized code, equivalent to + width=1/N; Sum=0; // for i=0:N-1 + i=0:N-1; // x=(i+0.5)*width; + x=(i+0.5)*width; // Sum=Sum+4/(1+x^2); + Sum=sum(4 ./ (1+x.^2)); // end + Sum = Sum/N ; // 1/N better at end: don't lose resolution + ////////////////////////////////////////////////////////////////// + //results.time = etime(clock,T); // + ////////////////////////////////////////////////////////////////// + results.err = Sum-%pi; + results.pi = Sum // ; + + + ////////////////////////////// + // FINAL CHECK // + ////////////////////////////// + if abs(results.err)>5E-10 + warning("pretty nice pi value! go fix it") + end + disp(results) +endfunction + + diff --git a/modules/mpi/examples/foo.sci b/modules/mpi/examples/foo.sci new file mode 100755 index 000000000..875197c6e --- /dev/null +++ b/modules/mpi/examples/foo.sci @@ -0,0 +1,15 @@ +MPI_Init(); +rnk = MPI_Comm_rank() +sizeNodes = MPI_Comm_size(); +SLV = rnk; +Master = ~ SLV; + +if Master then + disp("Master"); + MPI_Send(rand(10,10), 1); +else + val = MPI_Recv(0, 0); + disp(val, rnk) +end + +MPI_Finalize(); diff --git a/modules/mpi/examples/simple.sci b/modules/mpi/examples/simple.sci new file mode 100755 index 000000000..908884f99 --- /dev/null +++ b/modules/mpi/examples/simple.sci @@ -0,0 +1,9 @@ +function simple() + disp("simple"); + MPI_Init(); + disp(MPI_Comm_rank()); // let it abort if it fails + disp(MPI_Comm_size()); + disp("simple 2"); + MPI_Finalize() + +endfunction
\ No newline at end of file |