-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f92eba
commit 0a6e7eb
Showing
44 changed files
with
2,760 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# parallel2 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
OBJS=main.cpp | ||
MPICC?=mpic++ | ||
|
||
release: ${OBJS} | ||
${MPICC} -O3 ${OBJS} -o release.out | ||
|
||
run: | ||
mpiexec -n 4 release.out | ||
|
||
clean: | ||
rm -f *.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <iostream> | ||
#include <mpi.h> | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
MPI_Init(&argc, &argv); | ||
|
||
int rank, world_size, data; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
MPI_Comm_size(MPI_COMM_WORLD, &world_size); | ||
|
||
MPI_Send(&rank, 1, MPI_INT, (rank + 1) % world_size, 0, MPI_COMM_WORLD); | ||
MPI_Recv(&data, 1, MPI_INT, ((rank - 1) + world_size) % world_size, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | ||
|
||
std::cout << "I am " << rank << " of " << world_size << " and received " << data << ".\n"; | ||
|
||
MPI_Finalize(); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
OBJS=main.cpp | ||
MPICC?=mpic++ | ||
|
||
release: ${OBJS} | ||
${MPICC} -O3 -std=c++14 ${OBJS} -o release.out | ||
|
||
run: | ||
mpiexec --oversubscribe -n 16 release.out | ||
|
||
clean: | ||
rm -f *.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include <cmath> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <mpi.h> | ||
|
||
enum class ShuffExIcf | ||
{ | ||
shffle, | ||
xchange | ||
}; | ||
|
||
enum class pm2iIcf | ||
{ | ||
PLUS, | ||
MINUS | ||
}; | ||
|
||
int mask(std::string maskStr) | ||
{ | ||
int rank; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
|
||
for (auto itMask = std::rbegin(maskStr); itMask != std::rend(maskStr); | ||
++itMask, rank >>= 1) | ||
{ | ||
if (*itMask == 'x' | *itMask == 'X') continue; | ||
|
||
bool ithBitZero = rank % 2 == 0; | ||
|
||
if (ithBitZero != (*itMask == '0')) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
int pm2i(std::string const& m, enum pm2iIcf icf, int i, int data) | ||
{ | ||
if (!mask(m)) return data; | ||
|
||
int rank, size; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
|
||
int source, dest; | ||
switch (icf) | ||
{ | ||
case pm2iIcf::PLUS: | ||
dest = (rank + (int)std::pow(2, i)) % size; | ||
source = (rank + size - (int)std::pow(2, i)) % size; | ||
break; | ||
case pm2iIcf::MINUS: | ||
dest = (rank + size - (int)std::pow(2, i)) % size; | ||
source = (rank + (int)std::pow(2, i)) % size; | ||
break; | ||
} | ||
|
||
MPI_Send(&data, 1, MPI_INT, dest, 0, MPI_COMM_WORLD); | ||
MPI_Recv(&data, 1, MPI_INT, source, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | ||
|
||
return data; | ||
} | ||
|
||
int shuffleExchange(std::string msk, ShuffExIcf icf, int data) | ||
{ | ||
if (!mask(msk)) return data; | ||
|
||
int rank, size; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
int m = log2(size); | ||
|
||
if (icf == ShuffExIcf::shffle) | ||
{ | ||
int A; | ||
int tmp; | ||
if (mask("XXXXXXX0")) { A = data; } | ||
tmp = pm2i("XXXXXXXX", pm2iIcf::PLUS, 0, data); | ||
if (!mask("XXXXXXX1")) { data = tmp; } | ||
for (auto j = 1; j < m; ++j) | ||
{ | ||
msk = "XXXXXXXX"; | ||
msk[msk.size() - 1 - j] = '1'; | ||
if (mask(msk)) { std::swap(data, A); } | ||
tmp = pm2i("XXXXXXXX", pm2iIcf::PLUS, j, data); | ||
if (mask("XXXXXXX0")) { data = tmp; } | ||
} | ||
tmp = pm2i("XXXXXXXX", pm2iIcf::PLUS, 0, data); | ||
if (!mask("XXXXXXX0")) { data = tmp; } | ||
if (mask("XXXXXXX0")) { data = A; } | ||
} | ||
else if (icf == ShuffExIcf::xchange) | ||
{ | ||
data = pm2i("XXXXXXXX", pm2iIcf::PLUS, 0, data); | ||
data = pm2i("XXXXXXX0", pm2iIcf::MINUS, 1, data); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
void printRunning(const char* m, int running[64]) | ||
{ | ||
int rank, size; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
std::cout << m << ": "; | ||
for (int i = 0; i < size; ++i) | ||
{ | ||
std::cout << std::setw(3) << running[i]; | ||
} | ||
std::cout << std::endl; | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
int rank, size; | ||
int data; | ||
int running[64]; | ||
const char* m = "XXXXXXXX"; | ||
MPI_Init(&argc, &argv); | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
|
||
if (!rank) std::cout << "\nshuffle:\n"; | ||
data = shuffleExchange(m, ShuffExIcf::shffle, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m, running); | ||
MPI_Barrier(MPI_COMM_WORLD); | ||
|
||
if (!rank) std::cout << "\nexchange:\n"; | ||
data = shuffleExchange(m, ShuffExIcf::xchange, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m, running); | ||
MPI_Barrier(MPI_COMM_WORLD); | ||
|
||
MPI_Finalize(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
OBJS=main.cpp | ||
MPICC?=mpic++ | ||
|
||
release: ${OBJS} | ||
${MPICC} -std=c++14 -O3 ${OBJS} -o release.out | ||
|
||
run: | ||
mpiexec --oversubscribe -n 8 release.out | ||
|
||
clean: | ||
rm -f *.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
#include <cmath> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <mpi.h> | ||
|
||
enum class pm2iIcf | ||
{ | ||
PLUS, | ||
MINUS | ||
}; | ||
|
||
int mask(std::string maskStr) | ||
{ | ||
int rank; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
|
||
for (auto itMask = std::rbegin(maskStr); itMask != std::rend(maskStr); | ||
++itMask, rank >>= 1) | ||
{ | ||
if (*itMask == 'x' | *itMask == 'X') continue; | ||
|
||
bool ithBitZero = rank % 2 == 0; | ||
|
||
if (ithBitZero != (*itMask == '0')) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
int cube(std::string const& m, int icf, int data) | ||
{ | ||
if (!mask(m)) return data; | ||
|
||
int rank; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
|
||
auto dest = rank ^ (1 << icf); | ||
|
||
MPI_Send(&data, 1, MPI_INT, dest, 0, MPI_COMM_WORLD); | ||
MPI_Recv(&data, 1, MPI_INT, dest, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | ||
|
||
return data; | ||
} | ||
|
||
int pm2i(std::string msk, enum pm2iIcf icf, int i, int data) | ||
{ | ||
auto send = mask(msk); | ||
auto orig = data; | ||
|
||
int rank, size; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
int m = log2(size); | ||
|
||
auto digit = ' '; | ||
switch (icf) | ||
{ | ||
case pm2iIcf::PLUS: | ||
digit = '1'; | ||
break; | ||
case pm2iIcf::MINUS: | ||
digit = '0'; | ||
break; | ||
} | ||
|
||
auto xMask = std::string(m, 'X'); | ||
for (auto j = m - 1; j >= i; --j) | ||
{ | ||
msk = xMask; | ||
|
||
for (int c = i; c < j; ++c) | ||
msk[msk.length() - 1 - c] = digit; | ||
|
||
auto tmp = cube(xMask, j, data); | ||
|
||
if (mask(msk)) data = tmp; | ||
} | ||
|
||
if (send) | ||
return data; | ||
else | ||
return orig; | ||
} | ||
|
||
void printRunning(const char* m, int running[64]) | ||
{ | ||
int rank, size; | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
std::cout << m << ": "; | ||
for (int i = 0; i < size; ++i) | ||
{ | ||
std::cout << std::setw(3) << running[i]; | ||
} | ||
std::cout << std::endl; | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
int rank, size; | ||
int data; | ||
int running[64]; | ||
const char* m = "XXXXXXXX"; | ||
MPI_Init(&argc, &argv); | ||
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
|
||
if ((size & (size - 1))) | ||
{ | ||
if (rank == 0) | ||
{ | ||
std::cerr | ||
<< "\n\n\tThere must be a perfect power of 2 number of threads\n\n\n"; | ||
} | ||
|
||
MPI_Finalize(); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
if (!rank) std::cout << "\nplus 2^0:\n"; | ||
data = pm2i(m, pm2iIcf::PLUS, 0, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m, running); | ||
|
||
if (!rank) std::cout << "\nplus 2^1:\n"; | ||
data = pm2i(m, pm2iIcf::PLUS, 1, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m, running); | ||
|
||
if (!rank) std::cout << "\nplus 2^2:\n"; | ||
data = pm2i(m, pm2iIcf::PLUS, 2, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m, running); | ||
|
||
if (!rank) std::cout << "\nminus 2^0:\n"; | ||
data = pm2i(m, pm2iIcf::MINUS, 0, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m, running); | ||
|
||
if (!rank) std::cout << "\nminus 2^1:\n"; | ||
data = pm2i(m, pm2iIcf::MINUS, 1, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m, running); | ||
|
||
if (!rank) std::cout << "\nminus 2^2:\n"; | ||
data = pm2i(m, pm2iIcf::MINUS, 2, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m, running); | ||
|
||
const char* m1 = "XXXXXXX0"; | ||
if (!rank) std::cout << "\nplus 2^1:\n"; | ||
data = pm2i(m1, pm2iIcf::PLUS, 1, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m1, running); | ||
|
||
const char* m2 = "XXXXXXX1"; | ||
if (!rank) std::cout << "\nplus 2^1:\n"; | ||
data = pm2i(m2, pm2iIcf::PLUS, 1, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m2, running); | ||
|
||
const char* m3 = "XXXXXX00"; | ||
if (!rank) std::cout << "\nplus 2^2:\n"; | ||
data = pm2i(m3, pm2iIcf::PLUS, 2, rank); | ||
MPI_Gather(&data, 1, MPI_INT, running, 1, MPI_INT, 0, MPI_COMM_WORLD); | ||
if (rank == 0) printRunning(m3, running); | ||
|
||
MPI_Finalize(); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
OBJS=main.cpp | ||
MPICC?=mpic++ | ||
|
||
release: ${OBJS} | ||
${MPICC} -O3 ${OBJS} -o release.out | ||
|
||
run: | ||
mpiexec --oversubscribe -n 16 release.out | ||
|
||
clean: | ||
rm -f *.out |
Oops, something went wrong.