forked from gtfock-chem/GTMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGTMatrix_Other.c
159 lines (147 loc) · 4.78 KB
/
GTMatrix_Other.c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <mpi.h>
#include <complex.h>
#include "GTMatrix_Retval.h"
#include "GTMatrix_Typedef.h"
#include "GTMatrix_Get.h"
#include "GTMatrix_Other.h"
int GTM_sync(GTMatrix_t gtm)
{
if (gtm == NULL) return GTM_NULL_PTR;
if (SYNC_IBARRIER == 1)
{
// Observed that on some Skylake & KNL machine with IMPI 17, when not all
// MPI processes have RMA calls, a MPI_Barrier will lead to deadlock when
// running more than 16 MPI processes on the same node. Don't know why using
// a MPI_Ibarrier + MPI_Ibarrier can solve this problem...
MPI_Status status;
MPI_Request req;
MPI_Ibarrier(gtm->mpi_comm, &req);
MPI_Wait(&req, &status);
} else {
MPI_Barrier(gtm->mpi_comm);
}
return GTM_SUCCESS;
}
int GTM_waitNB(GTMatrix_t gtm)
{
if (gtm == NULL) return GTM_NULL_PTR;
for (int dst_rank = 0; dst_rank < gtm->comm_size; dst_rank++)
{
if (gtm->nb_op_proc_cnt[dst_rank] != 0)
{
MPI_Win_unlock(dst_rank, gtm->mpi_win);
gtm->nb_op_proc_cnt[dst_rank] = 0;
}
}
gtm->nb_op_cnt = 0;
return GTM_SUCCESS;
}
int GTM_fill(GTMatrix_t gtm, void *value)
{
if (gtm == NULL) return GTM_NULL_PTR;
if (gtm->unit_size == 4)
{
int _value, *ptr;
memcpy(&_value, value, 4);
ptr = (int*) gtm->mat_block;
for (int i = 0; i < gtm->my_nrows; i++)
{
int offset_i = i * gtm->ld_local;
for (int j = 0; j < gtm->my_ncols; j++)
ptr[offset_i + j] = _value;
}
}
if (gtm->unit_size == 8)
{
double _value, *ptr;
memcpy(&_value, value, 8);
ptr = (double*) gtm->mat_block;
for (int i = 0; i < gtm->my_nrows; i++)
{
int offset_i = i * gtm->ld_local;
for (int j = 0; j < gtm->my_ncols; j++)
ptr[offset_i + j] = _value;
}
}
if (gtm->unit_size == 16)
{
double _Complex _value, *ptr;
memcpy(&_value, value, 16);
ptr = (double _Complex*) gtm->mat_block;
for (int i = 0; i < gtm->my_nrows; i++)
{
int offset_i = i * gtm->ld_local;
for (int j = 0; j < gtm->my_ncols; j++)
ptr[offset_i + j] = _value;
}
}
return GTM_SUCCESS;
}
int GTM_symmetrize(GTMatrix_t gtm)
{
if (gtm == NULL) return GTM_NULL_PTR;
if (gtm->nrows != gtm->ncols) return GTM_NOT_SQUARE_MAT;
// This process holds [rs:re, cs:ce], need to fetch [cs:ce, rs:re]
void *rcv_buf = gtm->symm_buf;
int my_row_start = gtm->r_displs[gtm->my_rowblk];
int my_col_start = gtm->c_displs[gtm->my_colblk];
GTM_sync(gtm);
GTM_getBlock(
gtm, my_col_start, gtm->my_ncols,
my_row_start, gtm->my_nrows,
rcv_buf, gtm->my_nrows
);
// Wait all processes to get the symmetric block before modifying
// local block, or some processes will get the modified block
GTM_sync(gtm);
if (MPI_INT == gtm->datatype)
{
int *src_buf = (int*) gtm->mat_block;
int *dst_buf = (int*) rcv_buf;
for (int irow = 0; irow < gtm->my_nrows; irow++)
{
for (int icol = 0; icol < gtm->my_ncols; icol++)
{
int idx_s = irow * gtm->ld_local + icol;
int idx_d = icol * gtm->my_nrows + irow;
src_buf[idx_s] += dst_buf[idx_d];
src_buf[idx_s] /= 2;
}
}
}
if (MPI_DOUBLE == gtm->datatype)
{
double *src_buf = (double*) gtm->mat_block;
double *dst_buf = (double*) rcv_buf;
for (int irow = 0; irow < gtm->my_nrows; irow++)
{
for (int icol = 0; icol < gtm->my_ncols; icol++)
{
int idx_s = irow * gtm->ld_local + icol;
int idx_d = icol * gtm->my_nrows + irow;
src_buf[idx_s] += dst_buf[idx_d];
src_buf[idx_s] *= 0.5;
}
}
}
if (MPI_C_DOUBLE_COMPLEX == gtm->datatype)
{
double _Complex *src_buf = (double _Complex*) gtm->mat_block;
double _Complex *dst_buf = (double _Complex*) rcv_buf;
for (int irow = 0; irow < gtm->my_nrows; irow++)
{
for (int icol = 0; icol < gtm->my_ncols; icol++)
{
int idx_s = irow * gtm->ld_local + icol;
int idx_d = icol * gtm->my_nrows + irow;
src_buf[idx_s] += conj(dst_buf[idx_d]);
src_buf[idx_s] *= 0.5;
}
}
}
return GTM_sync(gtm);
}