Skip to content
Snippets Groups Projects
Commit b75407fe authored by Nicolas Fley's avatar Nicolas Fley
Browse files

pi, token, bcast, bcast_log done

parent af0e6ab0
Branches
No related tags found
No related merge requests found
Showing
with 353 additions and 49 deletions
rapports/*
exercice_bcast_Nicolas_FLEY/execution.png

65.7 KiB

#include <stdio.h>
#include <mpi.h>
/*
Commands :
cd 'B:\Mes Documents\progra\calcInt\'
C:\MPICH2\bin\mpiexec.exe -localonly 10
*/
int main (int argc, char *argv[]) {
int rank, size;
MPI_Status status;
int test=0;
int message_to_send=7;
MPI_Init (&argc, &argv); /* starts MPI */
if(argc==2){
message_to_send = atoi(argv[1]);
}
MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size(MPI_COMM_WORLD, &size); /* get number of processes */
if(rank==0){
for(int i=1;i!=size;i++)
MPI_Send(&message_to_send,1,MPI_INT,i,10,MPI_COMM_WORLD);
}else{
MPI_Recv(&message_to_send,1,MPI_INT,0,10,MPI_COMM_WORLD,&status);
printf("%d received by %d\n",message_to_send,rank);
}
MPI_Finalize();
}
#include <stdio.h>
#include <mpi.h>
#include <math.h>
/*
Commands :
cd 'B:\Mes Documents\progra\calcInt\'
C:\MPICH2\bin\mpiexec.exe -localonly 10
*/
// explications given at the bottom of this file
int whoSendNext(int loop,int rank); // to know who to send next
int whoSendBef(int rank); // to know who send the message to the process
int main (int argc, char *argv[]) {
int rank, size, next, numLoopRecv;
MPI_Status status;
int test=0;
int message_to_send=7;
int numLoop=1;
MPI_Init (&argc, &argv); /* starts MPI */
if(argc==2){
message_to_send = atoi(argv[1]);
}
/*
for(int i=1;i!=10;i++){
numLoopRecv=((int)(log2((double)i)+2.01));
printf("###\n numLoopRecv : %d\n",numLoopRecv);
printf("\nrank %d wait for %d\n",i,whoSendBef(numLoopRecv,i));
printf("\nrank %d send for %d\n",i,whoSendNext(numLoopRecv,i));
}
return 0;
*/
MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size(MPI_COMM_WORLD, &size); /* get number of processes */
int numMaxLoop=((int)log2((double)size)+2.01); // get the rank of the final loop
if(!(rank==0 && numLoop==1)){
numLoop=((int)(log2((double)rank)+2.01)); // get the rank of the loop it will be when the process will receive the message
printf("%d wait for reception from %d\n",rank,whoSendBef(numLoopRecv,rank));
MPI_Recv(&message_to_send,1,MPI_INT,whoSendBef(numLoop,rank),10,MPI_COMM_WORLD,&status);
}
while(numLoop<=numMaxLoop){
next=whoSendNext(numLoop,rank);
if(next<size){ // will stop broadcast when all process have been reached
printf("%d wait for a sending to %d\n",rank,next);
MPI_Send(&message_to_send,1,MPI_INT,whoSendNext(numLoop,rank),10,MPI_COMM_WORLD);
}
numLoop+=1;
}
MPI_Finalize();
}
/*
* Logic used is :
* (turn : 1 2 3 4)
* 0 will send to 1 then 2 then 4 then 8
* 1 will send to 3 then 5 then 9
* 2 will send to 6 then 10
* 3 will send to 7 then 11
* ...
* n will send to ... n+2^(turn-1) then n+2^(turn) ...
*/
int whoSendNext(int loop,int rank){ // OK
int inc=((int)(pow(2.0,((double)loop)-1.0)+0.01));
return rank+inc;
}
int whoSendBef(int loop,int rank){
int inc=((int)(pow(2.0,((int)log2((double)rank)+0.01))+0.01));
return rank-inc;
}
File added
#include <stdio.h>
#include <mpi.h>
/*
Commands :
cd 'B:\Mes Documents\progra\calcInt\'
C:\MPICH2\bin\mpiexec.exe -localonly 10
*/
int main (int argc, char *argv[]) {
int rank, size;
MPI_Status status;
int test=0;
int message_to_send=7;
MPI_Init (&argc, &argv); /* starts MPI */
if(argc==2){
message_to_send = atoi(argv[1]);
}
MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size(MPI_COMM_WORLD, &size); /* get number of processes */
if(rank==0){
for(int i=1;i!=size;i++)
MPI_Send(&message_to_send,1,MPI_INT,i,10,MPI_COMM_WORLD);
}else{
MPI_Recv(&message_to_send,1,MPI_INT,0,10,MPI_COMM_WORLD,&status);
printf("%d received by %d\n",message_to_send,rank);
}
MPI_Finalize();
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="mpi_bcast" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/mpi_bcast" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/mpi_bcast" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="main.c">
<Option compilerVar="CC" />
</Unit>
<Extensions>
<code_completion />
<envvars />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
File added
File added
mpi_bcast_log/execution.png

65.7 KiB

#include <stdio.h>
#include <mpi.h>
#include <math.h>
/*
Commands :
cd 'B:\Mes Documents\progra\calcInt\'
C:\MPICH2\bin\mpiexec.exe -localonly 10
*/
// explications given at the bottom of this file
int whoSendNext(int loop,int rank); // to know who to send next
int whoSendBef(int rank); // to know who send the message to the process
int main (int argc, char *argv[]) {
int rank, size, next, numLoopRecv;
MPI_Status status;
int test=0;
int message_to_send=7;
int numLoop=1;
MPI_Init (&argc, &argv); /* starts MPI */
if(argc==2){
message_to_send = atoi(argv[1]);
}
/*
for(int i=1;i!=10;i++){
numLoopRecv=((int)(log2((double)i)+2.01));
printf("###\n numLoopRecv : %d\n",numLoopRecv);
printf("\nrank %d wait for %d\n",i,whoSendBef(numLoopRecv,i));
printf("\nrank %d send for %d\n",i,whoSendNext(numLoopRecv,i));
}
return 0;
*/
MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size(MPI_COMM_WORLD, &size); /* get number of processes */
int numMaxLoop=((int)log2((double)size)+2.01); // get the rank of the final loop
if(!(rank==0 && numLoop==1)){
numLoop=((int)(log2((double)rank)+2.01)); // get the rank of the loop it will be when the process will receive the message
printf("%d wait for reception from %d\n",rank,whoSendBef(numLoopRecv,rank));
MPI_Recv(&message_to_send,1,MPI_INT,whoSendBef(numLoop,rank),10,MPI_COMM_WORLD,&status);
}
while(numLoop<=numMaxLoop){
next=whoSendNext(numLoop,rank);
if(next<size){ // will stop broadcast when all process have been reached
printf("%d wait for a sending to %d\n",rank,next);
MPI_Send(&message_to_send,1,MPI_INT,whoSendNext(numLoop,rank),10,MPI_COMM_WORLD);
}
numLoop+=1;
}
MPI_Finalize();
}
/*
* Logic used is :
* (turn : 1 2 3 4)
* 0 will send to 1 then 2 then 4 then 8
* 1 will send to 3 then 5 then 9
* 2 will send to 6 then 10
* 3 will send to 7 then 11
* ...
* n will send to ... n+2^(turn-1) then n+2^(turn) ...
*/
int whoSendNext(int loop,int rank){ // OK
int inc=((int)(pow(2.0,((double)loop)-1.0)+0.01));
return rank+inc;
}
int whoSendBef(int loop,int rank){
int inc=((int)(pow(2.0,((int)log2((double)rank)+0.01))+0.01));
return rank-inc;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="mpi_bcast_log" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/mpi_bcast_log" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/mpi_bcast_log" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="main.c">
<Option compilerVar="CC" />
</Unit>
<Extensions>
<code_completion />
<envvars />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1487791165 source:b:\mes documents\progra\calcint\mpi_bcast_log\main.c
<stdio.h>
<mpi.h>
<math.h>
File added
No preview for this file type
#include <stdio.h>
#include <mpi.h>
#include <time.h>
#define PI 3.14159265359
double sequential(int bnTrap);
double sequential_boucle(int bnTrap, int nbBoucle);
double parralele(int nbTrap);
double f(double x);
int main (int argc, char *argv[]) {
//printf("pi = %f\n",sequential(100));
//printf("pi= %f\n",sequential_boucle(100,10));
MPI_Init (&argc, &argv); /* starts MPI */
printf("pi= %f\n",parralele(1000));
clock_t begin,end = clock();
printf("pi_seq = %f\n",sequential(10000));
printf("pi_bouc = %f\n",sequential_boucle(10000,10));
/*printf("sequential version\n");
for(int i=1;i!=33554432*8;i*=2){
begin = clock();
printf("%d,%.10f,",i,sequential(i)-PI);
end = clock();
printf("%f\n",(double)(end-begin)/CLOCKS_PER_SEC);
}
printf("sequential ready for parallelisation version\n");
for(int i=1;i!=33554432*8;i*=2){
begin = clock();
printf("%d,%.10f,",i,sequential_boucle(i,10)-PI);
end = clock();
printf("%.10f\n",(double)(end-begin)/CLOCKS_PER_SEC);
}*/
return 0;
}
......@@ -55,46 +71,3 @@ double sequential_boucle(int nbTrap, int nbBoucle){
}
return sum/nbTrap_d/2.0; // reduction
}
double parralele(int nbTrap){
/*
* Version sequentielle du probleme, appel de n processus calculant une partie
* de l'intégrale définie précedemment. Puis renvoie au master (rank==0) qui
* agrège tout ça.
*/
// les variables
int rank, size, nbProcess;
double pi_esc,nbProcess_f,h;
MPI_Status status;
double sum=0;
// recuperation pour chaque process de son numero et du nombre de process
MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size(MPI_COMM_WORLD, &size); /* get number of processes */
nbProcess=size-1;
nbProcess_f=(double)size-1.0;
h=1.0/(double)nbTrap;
if(rank==0){
printf("\nexecuted with %f processes",nbProcess_f);
for(int i=1;i!=nbProcess+1;i++){
MPI_Recv(&pi_esc,1,MPI_DOUBLE,i,10,MPI_COMM_WORLD,&status);
printf("\nrecu proc %d = %f",i,pi_esc);
sum+=pi_esc;
}
return sum;
}else{
double b=(double)rank-1.0;
pi_esc=0;
for(double i=b/nbProcess_f;i<(b+1.0)/nbProcess_f;i+=h){ // séparation en boucle
pi_esc+=(f(i)+f((i+h))); //somme
}
pi_esc=pi_esc/((double)nbTrap)/2.0;
MPI_Send(&pi_esc,1,MPI_DOUBLE,0,10,MPI_COMM_WORLD);
printf("\nsent proc %d = %f",rank,pi_esc);
}
MPI_Finalize();
}
No preview for this file type
......@@ -31,6 +31,9 @@
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="main.c">
<Option compilerVar="CC" />
</Unit>
<Extensions>
<code_completion />
<envvars />
......
# depslib dependency file v1.0
1487185591 source:b:\mes documents\progra\calcint\pi\main.c
1487441616 source:b:\mes documents\progra\calcint\pi\main.c
<stdio.h>
<mpi.h>
<time.h>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.c" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="366" topLine="1" />
</Cursor>
</File>
</CodeBlocks_layout_file>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment