| 1 | #include<mpi.h>
|
|---|
| 2 | #include<civlc.cvh>
|
|---|
| 3 | #include<stdio.h>
|
|---|
| 4 |
|
|---|
| 5 | #pragma CIVL ACSL
|
|---|
| 6 | #define DATA_LIMIT 1024
|
|---|
| 7 |
|
|---|
| 8 | int left, right, nxl, nx, rank, nprocs;
|
|---|
| 9 | double * u, * u_new, k;
|
|---|
| 10 |
|
|---|
| 11 | #define OWNER(index) ((nprocs*(index+1)-1)/nx)
|
|---|
| 12 |
|
|---|
| 13 | #define FIRST(rank) ((rank)*nx/nprocs)
|
|---|
| 14 |
|
|---|
| 15 | #define LOCAL_OF(index) (index - (OWNER(index)*nx/nprocs) + 1)
|
|---|
| 16 |
|
|---|
| 17 | #define READ(index) (\on(OWNER(index), u)[LOCAL_OF(index)])
|
|---|
| 18 |
|
|---|
| 19 | /* TODO: ideally the requirements for left and right neighbor shall be
|
|---|
| 20 | * that left != rank && right != rank &&
|
|---|
| 21 | * the set L { l | \on(left, i), 0 <= i < \mpi_comm_size} equals
|
|---|
| 22 | * to the set process rank set R {0, 1, ..., \mpi_comm_size-1, MPI_PROC_NULL}.
|
|---|
| 23 | * ditto for 'right'.
|
|---|
| 24 | *
|
|---|
| 25 | * But currently we can only assume that data is distributed in a typical way.
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | /*@ \mpi_collective(MPI_COMM_WORLD, P2P):
|
|---|
| 29 | @ requires rank == \mpi_comm_rank;
|
|---|
| 30 | @ requires nxl > 0 && nxl < DATA_LIMIT;
|
|---|
| 31 | @ requires \mpi_valid(u, nxl + 2, MPI_DOUBLE);
|
|---|
| 32 | @ requires left != \mpi_comm_rank &&
|
|---|
| 33 | @ ( 0 <= left && left < \mpi_comm_size ||
|
|---|
| 34 | @ left == MPI_PROC_NULL );
|
|---|
| 35 | @ requires right != \mpi_comm_rank &&
|
|---|
| 36 | @ ( 0 <= right && right < \mpi_comm_size ||
|
|---|
| 37 | @ right == MPI_PROC_NULL );
|
|---|
| 38 | @ behavior hasLeft:
|
|---|
| 39 | @ assumes left != MPI_PROC_NULL;
|
|---|
| 40 | @ requires rank == \on(left, right); // I'm the 'right' of my left
|
|---|
| 41 | @ assigns \mpi_region(u, 1, MPI_DOUBLE);
|
|---|
| 42 | @ ensures \on(left, u[nxl]) == u[0] && \on(left, u[nxl+1]) == u[1];
|
|---|
| 43 | @ waitsfor left;
|
|---|
| 44 | @ behavior hasRight:
|
|---|
| 45 | @ assumes right != MPI_PROC_NULL;
|
|---|
| 46 | @ requires rank == \on(right, left); // I'm the 'left' of my right
|
|---|
| 47 | @ assigns \mpi_region(&u[nxl+1], 1, MPI_DOUBLE);
|
|---|
| 48 | @ ensures \on(right, u[1]) == u[nxl + 1] && \on(right, u[1]) == u[nxl]; // intentianal error
|
|---|
| 49 | @ waitsfor right;
|
|---|
| 50 | @*/
|
|---|
| 51 | void exchange_ghost_cells() {
|
|---|
| 52 | MPI_Sendrecv(&u[1], 1, MPI_DOUBLE, left, 0,
|
|---|
| 53 | &u[nxl+1], 1, MPI_DOUBLE, right, 0,
|
|---|
| 54 | MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 55 | // Incorrectly swap left and right
|
|---|
| 56 | MPI_Sendrecv(&u[nxl], 1, MPI_DOUBLE, left, 0,
|
|---|
| 57 | &u[0], 1, MPI_DOUBLE, right, 0,
|
|---|
| 58 | MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 59 | printf("rank=%d, left=%d, right=%d\n", rank, left, right);
|
|---|
| 60 | }
|
|---|