2023-05-08 13:22:41 +00:00
|
|
|
#define N 2
|
2023-05-08 12:15:13 +00:00
|
|
|
#define LENGTH 3
|
|
|
|
#define R 2
|
|
|
|
|
2023-05-08 12:31:13 +00:00
|
|
|
int to_reverse[LENGTH]; // array to reverse
|
2023-05-08 14:15:17 +00:00
|
|
|
int reversed_seq[LENGTH]; // for SequentialReverser: where the reverse is stored
|
|
|
|
int reversed_par[LENGTH]; // for ParallelReverser: where the reverse is stored
|
2023-05-08 12:31:13 +00:00
|
|
|
|
2023-05-08 14:15:17 +00:00
|
|
|
// stores termination of each reverser
|
|
|
|
// index 0 is for the sequential algorithm
|
|
|
|
// indices 1-N are for the ThreadedReverser instances
|
|
|
|
bool done[N + 1]; // initialized to false
|
|
|
|
|
|
|
|
bool seq_eq_to_parallel = true;
|
2023-05-08 12:46:00 +00:00
|
|
|
|
2023-05-08 12:31:13 +00:00
|
|
|
// ThreadedReverser implementation
|
2023-05-08 13:22:41 +00:00
|
|
|
proctype ThreadedReverser(int from; int to; int n) {
|
2023-05-08 12:46:00 +00:00
|
|
|
printf("proc[%d]: started\n", _pid);
|
|
|
|
|
2023-05-08 12:31:13 +00:00
|
|
|
int k;
|
|
|
|
for (k: from .. to) {
|
2023-05-08 14:15:17 +00:00
|
|
|
printf("reversed_par[%d] = to_reverse[%d]\n", LENGTH - k - 1, k);
|
|
|
|
reversed_par[LENGTH - k - 1] = to_reverse[k];
|
2023-05-08 12:46:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 13:22:41 +00:00
|
|
|
printf("proc[%d]: ended\n", n);
|
|
|
|
done[n] = true;
|
2023-05-08 12:31:13 +00:00
|
|
|
}
|
2023-05-08 12:15:13 +00:00
|
|
|
|
|
|
|
init {
|
2023-05-08 13:22:41 +00:00
|
|
|
printf("program start\n");
|
2023-05-08 12:15:13 +00:00
|
|
|
// array initialization
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i in to_reverse) {
|
|
|
|
int value;
|
|
|
|
select(value: 0 .. R);
|
|
|
|
to_reverse[i] = value;
|
2023-05-08 13:22:41 +00:00
|
|
|
printf("to_reverse[%d]: %d\n", i, value);
|
2023-05-08 12:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:15:17 +00:00
|
|
|
printf("sequential start\n");
|
|
|
|
// SequentialReverser implementation
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
for (k: 0 .. (LENGTH - 1)) {
|
|
|
|
reversed_par[LENGTH - k - 1] = to_reverse[k];
|
|
|
|
printf("reversed_seq[%d] = to_reverse[%d]\n", LENGTH - k - 1, k);
|
|
|
|
}
|
|
|
|
done[0] = true; // mark sequential end
|
2023-05-08 12:46:00 +00:00
|
|
|
}
|
2023-05-08 14:15:17 +00:00
|
|
|
printf("sequential end\n");
|
|
|
|
|
|
|
|
printf("parallel start\n");
|
|
|
|
// ParallelReverser implementation
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int s = LENGTH / N;
|
|
|
|
|
|
|
|
// fork section
|
|
|
|
for (n: 1 .. N) {
|
|
|
|
int from = n * s;
|
|
|
|
int to;
|
|
|
|
if
|
|
|
|
:: (n == N - 1) -> to = LENGTH - 1;
|
|
|
|
:: else -> to = n * s + s - 1;
|
|
|
|
fi
|
|
|
|
run ThreadedReverser(from, to, n); // run as "thread n"
|
|
|
|
}
|
2023-05-08 12:46:00 +00:00
|
|
|
|
2023-05-08 14:15:17 +00:00
|
|
|
// join section
|
|
|
|
for (n: 1 .. N) {
|
|
|
|
(done[n] == true); // wait "thread n"
|
|
|
|
printf("[%d] joined\n", n);
|
|
|
|
}
|
2023-05-08 13:22:41 +00:00
|
|
|
}
|
2023-05-08 14:15:17 +00:00
|
|
|
printf("parallel end\n");
|
2023-05-08 12:46:00 +00:00
|
|
|
|
2023-05-08 14:15:17 +00:00
|
|
|
// test if seq and parallel are the same
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i: 0 .. (LENGTH - 1)) {
|
|
|
|
if
|
|
|
|
:: (reversed_seq[i] != reversed_par[i]) -> seq_eq_to_parallel = false;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
2023-05-08 12:15:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 12:31:13 +00:00
|
|
|
// ltl syntax: https://spinroot.com/spin/Man/ltl.html
|
2023-05-08 14:15:17 +00:00
|
|
|
|
|
|
|
ltl seq_eq_parallel {
|
|
|
|
// the variable seq_eq_to_parallel will never be false if all the elements
|
|
|
|
// between the sequential and parallel reversed arrays are equal
|
|
|
|
(done[0] == false || done[N] == false) U (seq_eq_to_parallel == true)
|
|
|
|
}
|
|
|
|
|
|
|
|
ltl termination {
|
|
|
|
<> (done[0] == true && done[N] == true)
|
2023-05-08 12:15:13 +00:00
|
|
|
}
|