-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfield_util.cpp
More file actions
135 lines (130 loc) · 4.32 KB
/
Copy pathfield_util.cpp
File metadata and controls
135 lines (130 loc) · 4.32 KB
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
//****************************************************************************************
//
// Copyright (c) 2019-21, Ken-Ichi Ishikawa (ishikawa@theo.phys.sci.hirosima-u.ac.jp)
// Copyright (c) 2019, Issaku Kanamori (kanamori@hiroshima-u.ac.jp)
// Copyright (c) 2019-21, Issaku Kanamori (kanamori-i@riken.jp)
// Copyright (c) 2021, Yoshifumi Nakamura <nakamura@riken.jp>
// Copyright (c) 2021, Yuta Mukai <mukai.yuta@fujitsu.com>
//
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer listed
// in this license in the documentation and/or other materials
// provided with the distribution.
//
// * Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//----------------------------------------------------------------------------------------
// ACKNOWLEDGMENT
//
// This software has been developed in a co-design working group for the lattice QCD
// application on Post K computer and supported by Priority Issue 9 to be tackled
// by Using Post K Computer.
//
//****************************************************************************************
/*
some utilities: linear algebra related functions
Issaku Kanamori kanamori@hiroshima-u.ac.jp
2018 Dec.14, 2019 Feb. 6
*/
#include "config.h"
#include "comm_helper.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#ifdef _MPI_
#include <mpi.h>
#endif
namespace{
// thread global variables
double sum2;
double result;
}
// squared norm
double norm2(const double *in){
#pragma omp barrier
#pragma omp master
{
sum2=0.0;
result=0.0;
}
#pragma omp barrier
#pragma omp for reduction(+:sum2)
for(int i=0; i<Nx*Ny; i++){
sum2+=in[i]*in[i];
}
#pragma omp master
{
#ifdef _MPI_
communicator::timer[communicator::Timer::ALL_REDUCE].start();
MPI_Allreduce(&sum2, &result, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
communicator::timer[communicator::Timer::ALL_REDUCE].stop();
#else
result=sum2;
#endif
}
#pragma omp barrier
return result;
}
// initialize with random numbers
// for a given (NNx, NNy), this routine gives a unique value
// independet from (Px, Py) and gives the the same on in the serial version.
void set_field(double *v, int npx, int npy, int Px, int Py){
int idx=0;
int rankid=communicator::rankid;
if(npx<0){
npx=rankid % Px;
}
if(npy<0){
npy=rankid / Px;
}
double n2=0.0;
// printf("[npx=%d, npy=%d]\n",npx, npy);
//printf("rankid=%d, Px=%d, Py=%d\n", rankid, Px, Py);
NNx=Nx*Px;
NNy=Ny*Py;
srand(1);
for(int y=0; y<NNy; y++){
for(int x=0; x<NNx; x++){
double val=( (double) rand())/RAND_MAX;
n2+=val*val;
if(x / Nx != npx) { continue; }
if(y / Ny != npy) { continue; }
v[idx]=val;
idx++;
}
}
if(idx!=Nx*Ny){
printf("cannot happen [npx=%d, npy=%d], something is wrong: idx=%d (must be %d)\n",npx, npy, idx, Nx*Ny);
}
if(communicator::rankid==0){
printf("set_field: n2 before normalizing=%e\n", n2);
}
double factor=sqrt(1.0/n2);
for(int i=0; i<Nx*Ny; i++){
v[i]*=factor;
}
}