-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_grid.cc
executable file
·105 lines (85 loc) · 2.21 KB
/
block_grid.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "block_grid.h"
namespace RRR{
using namespace std;
// Constructor
Block_Grid::Block_Grid(int num_dims, int* block_grid)
{
grid_dims = num_dims;
bgrid = new int[grid_dims+1];
memcpy(bgrid, block_grid, grid_dims * sizeof(int));
MPI_Comm_size(MPI_COMM_WORLD,&nblocks);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
get_block_addr(rank, block_addr);
}
// Returns rank of a block given its multi-dimensional address
int Block_Grid::get_block_rank(int* &address)
{
int* addr = address;
int dims = grid_dims;
int* grid = bgrid;
int x = 1;
int r = 0;
for(int i=dims-1; i>=0; i--)
{
r += addr[i] * x;
x *= grid[i];
}
//cout << "rr = " << r << endl;
return r;
}
// Finds out ranks of n blocks given their multi-dimensional addresses
void Block_Grid::get_block_ranks(int n, int** &addresses, int* &ranks)
{
ranks = new int[n];
for(int r=0; r<n; r++)
{
ranks[r] = get_block_rank(addresses[r]);
}
}
// Computes a block's multi-dimensional address given its rank
void Block_Grid::get_block_addr(int r, int* &address)
{
address = new int[grid_dims];
int dims = grid_dims;
int* addr = address;
int* grid = bgrid;
int x = 1;
for(int i=0; i<dims; i++)
x *= grid[i];
for(int i=0; i<dims; i++)
{
x /= grid[i];
addr[i] = r/x;
r -= addr[i] * x;
}
}
// Computes n blocks' multi-dimensional addresses given their ranks
void Block_Grid::get_block_addrs(int n, int** &addresses, int* &ranks)
{
for(int r=0; r<n; r++)
{
get_block_addr(ranks[r], addresses[r]);
}
}
// prints address of a block given its rank
void Block_Grid::print_block_addr(int r)
{
cout << "node address is : " << get_block_addr_str(r) << endl;
}
// Returns the string of the block's multi-dimensional address, given its rank
string Block_Grid::get_block_addr_str(int r)
{
stringstream block_addr_str;
int* address;
get_block_addr(r, address);
block_addr_str << "[ ";
for(int i=0; i<grid_dims; i++)
{
block_addr_str << address[i];
if(i < grid_dims-1)
block_addr_str << ", ";
}
block_addr_str<<"]";
return block_addr_str.str();
}
}