/* * The Game of Life * * a cell is born, if it has exactly three neighbours * a cell dies of loneliness, if it has less than two neighbours * a cell dies of overcrowding, if it has more than three neighbours * a cell survives to the next generation, if it does not die of loneliness * or overcrowding * * In this version, a 2D array of ints is used. A 1 cell is on, a 0 cell is off. * The game plays a number of steps (given by the input), printing to the screen each time. 'x' printed * means on, space means off. * */ #include <stdio.h> #include <stdlib.h> #include <mpi.h> typedef unsigned char cell_t; cell_t ** allocate_board (int size) { cell_t ** board = (cell_t **) malloc(sizeof(cell_t*)*size); int i; for (i=0; i<size; i++) board[i] = (cell_t *) malloc(sizeof(cell_t)*size); return board; } void free_board (cell_t ** board, int size) { int i; for (i=0; i<size; i++) free(board[i]); free(board); } /* return the number of on cells adjacent to the i,j cell */ int adjacent_to (cell_t ** board, int size, int i, int j) { int k, l, count=0; int sk = (i>0) ? i-1 : i; int ek = (i+1 < size) ? i+1 : i; int sl = (j>0) ? j-1 : j; int el = (j+1 < size) ? j+1 : j; for (k=sk; k<=ek; k++) for (l=sl; l<=el; l++) count+=board[k][l]; count-=board[i][j]; return count; } /* print the life board */ void print (cell_t ** board, int size) { int i, j; /* for each row */ for (j=0; j<size; j++) { /* print each column position... */ for (i=0; i<size; i++) printf ("%c", board[i][j] ? 'x' : ' '); /* followed by a carriage return */ printf ("\n"); } } /* read a file into the life board */ void read_file (FILE * f, cell_t ** board, int size) { int i, j; char *s = (char *) malloc(size+10); char c; for (j=0; j<size; j++) { /* get a string */ fgets (s, size+10,f); /* copy the string to the life board */ for (i=0; i<size; i++) { //c=fgetc(f); //putchar(c); board[i][j] = s[i] == 'x'; } //fscanf(f,"\n"); } } int main (int argc, char** argv) { int size, steps, rank, threads, localInit, localEnd; MPI_Status status; FILE *f; cell_t ** prev; cell_t ** next; cell_t ** tmp; f = stdin; MPI_Init( &argc, &argv ); MPI_Comm_rank( MPI_COMM_WORLD , &rank ); MPI_Comm_size( MPI_COMM_WORLD , &threads ); fscanf(f,"%d %d", &size, &steps); MPI_Bcast( &size , 1 , MPI_INT , 0 , MPI_COMM_WORLD); *&next = allocate_board (size); *&prev = allocate_board (size); /* print (next,size); */ if (rank==0){ printf("size: %d, thread: %d\n", size, rank); printf("Cheguei aqui 1!! thread: %d\n", rank); /* *&prev = allocate_board (size); */ read_file (f, prev,size); fclose(f); /* print (prev,size); */ /* *&next = allocate_board (size); */ #ifdef DEBUG printf("Initial \n"); /* print(prev,size); */ printf("----------\n"); #endif for (int i=0; i<steps; i++) { /* play (prev,next,size); */ /* for each cell, apply the rules of Life */ for (int j = 1; j < threads; j++){ printf("Cheguei aqui 3!!\n"); MPI_Send( &next , size*size , MPI_UNSIGNED_CHAR , j , 1 , MPI_COMM_WORLD); } for (int j = 1; j < threads; j++){ MPI_Recv( &next , size*size , MPI_UNSIGNED_CHAR , j, 1 , MPI_COMM_WORLD , &status); printf("Cheguei aqui 4!! rank: %d\n", j); } #ifdef DEBUG printf("%d ----------\n", i); /* print (next,size); */ #endif tmp = next; next = prev; prev = tmp; } }else{ /* print (next,size); */ /* printf("size: %d, thread: %d\n", size, rank); */ printf("Cheguei aqui 2!! thread: %d\n", rank); MPI_Recv( &next , size*size , MPI_UNSIGNED_CHAR , 0, 1 , MPI_COMM_WORLD , &status); /* print (next,size); */ /* printf("\n\nTESTE: %c\n\n", next[0][0]); */ int chunk = size/(threads-2); printf("size: %d, threads: %d, chunk: %d\n" , size, threads, chunk); localInit = rank*chunk-chunk; localEnd = (rank+1)*chunk-chunk; if (rank==threads-1){ localEnd = size; } for (int i=localInit; i<localEnd; i++){ for (int j=0; j<size; j++) { /* printf("teste: %d\n\n" , next[0][0]); */ printf("rank: %d, localInit: %d, localEnd: %d, i: %d, j: %d\n" , rank, localInit, localEnd, i, j); int a = adjacent_to (prev, size, i, j); if (a == 2) next[i][j] = prev[i][j]; if (a == 3) next[i][j] = 1; if (a < 2) next[i][j] = 0; if (a > 3) next[i][j] = 0; printf("Cheguei aqui 5!!\n"); } } MPI_Send( &next , size*size , MPI_UNSIGNED_CHAR , 0 , 1 , MPI_COMM_WORLD); } print (prev,size); free_board(prev,size); free_board(next,size); MPI_Finalize(); }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}