| [7f8124c] | 1 | #include<mpi.h>
|
|---|
| [900fa2b] | 2 | #include<civlc.cvh>
|
|---|
| [1b7d18d] | 3 |
|
|---|
| [e93c797] | 4 | #pragma CIVL ACSL
|
|---|
| [1b7d18d] | 5 | #define DATA_LIMIT 1024
|
|---|
| [1b69190] | 6 |
|
|---|
| [e5d8c6f] | 7 | int left, right, nxl, nx, rank, nprocs;
|
|---|
| [b5cfaf6] | 8 | double * u, * u_new, k;
|
|---|
| [7f8124c] | 9 |
|
|---|
| [e5d8c6f] | 10 | #define OWNER(index) ((nprocs*(index+1)-1)/nx)
|
|---|
| 11 |
|
|---|
| [900fa2b] | 12 | #define LOCAL_OF(index) [index - (OWNER(index)*nx/nprocs) + 1]
|
|---|
| 13 |
|
|---|
| 14 | #define FIRST(rank) ((rank)*nx/nprocs)
|
|---|
| [8dfa3356] | 15 |
|
|---|
| 16 | #define READ(index) (\on(OWNER(index), u)LOCAL_OF(index))
|
|---|
| [e5d8c6f] | 17 |
|
|---|
| [f53d5c9] | 18 | /*@ \mpi_collective(MPI_COMM_WORLD, P2P):
|
|---|
| [7f8124c] | 19 | @ requires rank == \mpi_comm_rank;
|
|---|
| [1b7d18d] | 20 | @ requires nxl > 0 && nxl < DATA_LIMIT; //nxl shall not equal to zero
|
|---|
| [b5cfaf6] | 21 | @ requires \mpi_valid(u, nxl + 2, MPI_DOUBLE);
|
|---|
| [7f8124c] | 22 | @ behavior maxrank:
|
|---|
| [f53d5c9] | 23 | @ assumes rank == \mpi_comm_size - 1;
|
|---|
| [36420ee] | 24 | @ requires right == MPI_PROC_NULL && left == rank - 1;
|
|---|
| 25 | @ assigns \mpi_region(u, 1, MPI_DOUBLE);
|
|---|
| 26 | @ ensures \on(left, u[nxl]) == u[0];
|
|---|
| 27 | @ waitsfor left;
|
|---|
| [7f8124c] | 28 | @ behavior minrank:
|
|---|
| [f53d5c9] | 29 | @ assumes rank == 0;
|
|---|
| [36420ee] | 30 | @ requires left == MPI_PROC_NULL && right == rank + 1;
|
|---|
| 31 | @ assigns \mpi_region(&u[nxl+1], 1, MPI_DOUBLE);
|
|---|
| 32 | @ ensures \on(right, u[1]) == u[nxl + 1];
|
|---|
| 33 | @ waitsfor right;
|
|---|
| [7f8124c] | 34 | @ behavior others:
|
|---|
| [c0518e21] | 35 | @ assumes 0 < rank && rank < \mpi_comm_size - 1;
|
|---|
| [7f8124c] | 36 | @ requires left == rank - 1 && right == rank + 1;
|
|---|
| [36420ee] | 37 | @ assigns \mpi_region(u, 1, MPI_DOUBLE),
|
|---|
| 38 | @ \mpi_region(&u[nxl+1], 1, MPI_DOUBLE);
|
|---|
| 39 | @ ensures \on(left, u[nxl]) == u[0];
|
|---|
| 40 | @ ensures \on(right, u[1]) == u[nxl + 1];
|
|---|
| 41 | @ waitsfor left, right;
|
|---|
| [7f8124c] | 42 | @*/
|
|---|
| 43 | void exchange_ghost_cells() {
|
|---|
| 44 | MPI_Sendrecv(&u[1], 1, MPI_DOUBLE, left, 0,
|
|---|
| 45 | &u[nxl+1], 1, MPI_DOUBLE, right, 0,
|
|---|
| 46 | MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 47 | MPI_Sendrecv(&u[nxl], 1, MPI_DOUBLE, right, 0,
|
|---|
| 48 | &u[0], 1, MPI_DOUBLE, left, 0,
|
|---|
| 49 | MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| [cef0318] | 52 | /*@ requires \valid(u + (0 .. (nxl + 1)));
|
|---|
| 53 | @ requires \valid(u_new + (0 .. (nxl + 1)));
|
|---|
| [1b7d18d] | 54 | @ requires k > 0 && nxl > 0;
|
|---|
| [7608746] | 55 | @ assigns u[1 .. nxl];
|
|---|
| [885c088] | 56 | @ ensures \forall int i; 0< i && i <= nxl
|
|---|
| [7f8124c] | 57 | @ ==>
|
|---|
| 58 | @ u[i] == \old(u[i] + k*(u[i+1] + u[i-1] - 2*u[i]));
|
|---|
| 59 | @*/
|
|---|
| 60 | void update() {
|
|---|
| [1b7d18d] | 61 | /*@ loop invariant 1 <= i && i <= nxl + 1 &&
|
|---|
| 62 | @ \forall int j; 1 <= j && j < i ==>
|
|---|
| 63 | @ u_new[j] == u[j] + k*(u[j+1] + u[j-1] - 2*u[j]);
|
|---|
| 64 | @*/
|
|---|
| [7f8124c] | 65 | for (int i = 1; i <= nxl; i++)
|
|---|
| 66 | u_new[i] = u[i] + k*(u[i+1] + u[i-1] - 2*u[i]);
|
|---|
| 67 | double * tmp = u_new; u_new=u; u=tmp;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| [2216c99] | 70 | /*@
|
|---|
| [f53d5c9] | 71 | @ \mpi_collective(MPI_COMM_WORLD, P2P):
|
|---|
| [7f8124c] | 72 | @ requires rank == \mpi_comm_rank;
|
|---|
| [e5d8c6f] | 73 | @ requires nprocs == \mpi_comm_size;
|
|---|
| 74 | @ requires nxl > 0 && nxl < 5;
|
|---|
| 75 | @ requires nx > 5 && nx < 10;
|
|---|
| [f53d5c9] | 76 | @ requires \mpi_valid(u, nxl + 2, MPI_DOUBLE);
|
|---|
| 77 | @ requires \mpi_valid(u_new, nxl + 2, MPI_DOUBLE);
|
|---|
| [7f8124c] | 78 | @ requires nx == \sum(0, \mpi_comm_size - 1,
|
|---|
| [b5cfaf6] | 79 | @ (\lambda int k; \on(k, nxl)));
|
|---|
| [cef0318] | 80 | @ requires k > 0.0;
|
|---|
| [e5d8c6f] | 81 | @ requires \mpi_agree(nx) && \mpi_agree(k);
|
|---|
| [c66e452] | 82 | @ ensures \forall int i; 1 <= i && i < nx - 1
|
|---|
| [900fa2b] | 83 | @ ==>
|
|---|
| [8dfa3356] | 84 | @ READ(i) ==
|
|---|
| 85 | @ \old(
|
|---|
| 86 | @ READ(i) + k* (READ(i+1) + READ(i-1) - 2*READ(i))
|
|---|
| [e5d8c6f] | 87 | @ );
|
|---|
| [c66e452] | 88 | @ ensures READ(nx - 1) ==
|
|---|
| 89 | @ \old(
|
|---|
| 90 | @ READ(nx - 1) + k* (0 + READ(nx - 1 - 1) - 2*READ(nx - 1))
|
|---|
| 91 | @ );
|
|---|
| [9b1d574] | 92 | @ ensures READ(0) ==
|
|---|
| 93 | @ \old(
|
|---|
| 94 | @ READ(0) + k* (READ(0 + 1) + 0 - 2*READ(0))
|
|---|
| 95 | @ );
|
|---|
| [7f8124c] | 96 | @ behavior maxrank:
|
|---|
| [f53d5c9] | 97 | @ assumes rank == \mpi_comm_size - 1;
|
|---|
| [e5d8c6f] | 98 | @ requires right == MPI_PROC_NULL && left == rank - 1;
|
|---|
| [900fa2b] | 99 | @ requires u[nxl+1] == 0;
|
|---|
| 100 | @ requires nx - FIRST(\mpi_comm_rank) == nxl;
|
|---|
| [7f8124c] | 101 | @ behavior minrank:
|
|---|
| [f53d5c9] | 102 | @ assumes rank == 0;
|
|---|
| [e5d8c6f] | 103 | @ requires left == MPI_PROC_NULL && right == 1;
|
|---|
| [900fa2b] | 104 | @ requires u[0] == 0;
|
|---|
| 105 | @ requires FIRST(\mpi_comm_rank+1) == nxl;
|
|---|
| [7f8124c] | 106 | @ behavior others:
|
|---|
| [cef0318] | 107 | @ assumes 0 < rank && rank < \mpi_comm_size - 1;
|
|---|
| [7f8124c] | 108 | @ requires left == rank - 1 && right == rank + 1;
|
|---|
| [900fa2b] | 109 | @ requires nxl == FIRST(\mpi_comm_rank + 1) - FIRST(\mpi_comm_rank);
|
|---|
| [7f8124c] | 110 | @*/
|
|---|
| 111 | void diff1dIter() {
|
|---|
| [900fa2b] | 112 | $elaborate(nxl);
|
|---|
| 113 | exchange_ghost_cells();
|
|---|
| 114 | update();
|
|---|
| [7f8124c] | 115 | }
|
|---|