/* Computing the solver for heat equation in multithreads, then compare the * result with the solver got in a sequencial way. * * Command line example: * civl verify -inputNPROCSB=10 -inputK=0.3 -inputNSTEPS=5 -inputNX=10 diffusion1d.cvl -showAmpleSet * This diffusion1d program computing the diffusion1d equation in parallel and then compare the result with * another result of diffution1d equation computed in sequential. **/ #include #include $input int nprocs; $input int NPROCSB; $input double k; /* k = alpha^2 * dt/(dx^2) */ $input int nsteps; /* time */ $input int NSTEPSB; $input int nx; /* the length of the array */ $input int NXB; $input double initialU[nx]; double seq_u[nx]; $proc __procs[nprocs]; $gcomm __MPI_COMM_WORLD; $assume nx > nprocs; $assume 0 < nprocs && nprocs <= NPROCSB; $assume 0 < nsteps && nsteps <= NSTEPSB; $assume 2 < nx && nx <= NXB; void MPI_Process (int rank); void send(void *buf, int count, int dest, int tag, $comm comm) { $message out = $message_pack(comm->place, dest, tag, buf, count*sizeof(double)); $comm_enqueue(comm, out); } void recv(void *buf, int count, int source, int tag, $comm comm) { $message in = $comm_dequeue(comm, source, tag); $message_unpack(in, buf, count*sizeof(double)); } void init() { __MPI_COMM_WORLD = $gcomm_create($root, nprocs); for (int i=0; i0; iter--){ //update for(i=1; i= nprocs){ right = -1; } /* return the number of interior elements */ *first = start; *left_u = left; *right_u = right; return nxl; } /* the numbers of elements in u and u_new are 2 more than u_length because of ghost elements */ void initU(double * u, double * u_new, int nxl, int first, int left, int right){ int i, j; /* initiate interior array */ j = first; for(i=1; i< nxl+1; i++){ u[i] = initialU[j]; u_new[i] = initialU[j]; j++; } /* for the most left or right Us, initiate the first ghost element and the last ghost element */ if(left == -1){ u[0] = initialU[0]; u_new[0] = initialU[0]; } if(right == -1){ i = nxl + 1; u[i] = initialU[nx-1]; u_new[i] = initialU[nx-1]; } } void compare(int first, int nxl, double * u){ int i; for(i=0; i0; iter--){ exchange_ghost_cells(u, left_u, right_u, first, nxl, MPI_COMM_WORLD); update(u, u_new, first, nxl); } compare(first, nxl, u); }