void CFrog::on_load(CActor* actor){ for(int i = 0; i < POP_CELLS; ++i){ m_population[i] = 0.0f; } for(int i = 0; i < INF_CELLS; ++i){ m_inf_level[i] = 0.0f; } initialiseRNG(&m_state); /*get ramdom default position*/ frogHop(0,0, &m_pos_x, &m_pos_y, &m_state); }
void CFrog::hop(CActor* actor){ m_hops++; frogHop(m_pos_x, m_pos_y, &m_pos_x, &m_pos_y, &m_state); int newPosition = getCellFromPosition(m_pos_x, m_pos_y); m_send_buffer[0] = m_infected; #ifdef VERBOSE printf("Frog %ld hopped to %ld cell\n", m_rank, newPosition); #endif actor->send_message(m_send_buffer, newPosition+2, MSG_VISIT_CELL); /*Note: +2 added to new position to adjust for cell MPI process id * process 0 is environment * process 1 is timer * process 2 onwards cells * */ }
void frog_spin(int rank, int size, int nfrog, long * rng_state, MPI_Comm comm) { list frogs = NULL; int i; int start; int end; int chunk; int msg; chunk = nfrog/(size - 3); start = chunk * (rank - 3); end = start + chunk; if ( rank == size - 1 ) { end = nfrog; } /* Build initial list of frogs */ for ( i = start; i < end; i++ ) { frog f = make_frog(0,0); frogHop(0, 0, &f->x, &f->y, rng_state); /* random starting position */ push(f, &frogs); } i = 0; /* Loop as for cell list, looking for interrupts. To ensure we * don't get deadlocks, lots of magic happens in update_frog_list */ do { MPI_Status status; int flag; msg = -1; update_frog_list(&frogs, rng_state, comm); MPI_Iprobe(0, CONTROL_TAG, comm, &flag, &status); if ( flag ) { MPI_Recv(&msg, 1, MPI_INT, status.MPI_SOURCE, status.MPI_TAG, comm, &status); } if ( msg == YEAR_END ) { int nfrogs[2] = {0,0}; list it = frogs; /* Calculate global number of frogs (and diseased * frogs) */ while ( it ) { nfrogs[0]++; nfrogs[1] += ((frog)(it->data))->diseased; it = it->next; } /* Should do this by building a sub-communicator and doing * a reduction, but that's hard work */ if ( rank == 3 ) { int tmp[2]; int j; for ( j = 4; j < size; j++ ) { MPI_Recv(&tmp, 2, MPI_INT, j, FROG_REDUCTION, comm, &status); nfrogs[0] += tmp[0]; nfrogs[1] += tmp[1]; } printf("There are %d frogs (%d infected) in year %d\n", nfrogs[0], nfrogs[1], i++); } else { MPI_Send(&nfrogs, 2, MPI_INT, 3, FROG_REDUCTION, comm); } } } while ( msg != SIMULATION_END ) ; delete_list(&frogs, &delete_frog); }