| 1 |
# Copyright(C) 1999-2020, 2024 National Technology & Engineering Solutions |
| 2 |
# of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
# NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions are |
| 7 |
# met: |
| 8 |
# |
| 9 |
# * Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# * Redistributions in binary form must reproduce the above |
| 13 |
# copyright notice, this list of conditions and the following |
| 14 |
# disclaimer in the documentation and/or other materials provided |
| 15 |
# with the distribution. |
| 16 |
# |
| 17 |
# * Neither the name of NTESS nor the names of its |
| 18 |
# contributors may be used to endorse or promote products derived |
| 19 |
# from this software without specific prior written permission. |
| 20 |
# |
| 21 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
|
| 33 |
from phactori import * |
| 34 |
from paraview.simple import * |
| 35 |
|
| 36 |
#phactori_combine_to_single_python_file_subpiece_begin_1 |
| 37 |
|
| 38 |
global BarrierLockCount |
| 39 |
BarrierLockCount = 0 |
| 40 |
def BarrierLock(tagString = ""): |
| 41 |
"""called to sync up all processes""" |
| 42 |
global BarrierLockCount |
| 43 |
BarrierLockCount += 1 |
| 44 |
numProcs = SmartGetNumberOfProcesses() |
| 45 |
myPid = SmartGetLocalProcessId() |
| 46 |
localIntList = [myPid] |
| 47 |
if PhactoriDbg(100): |
| 48 |
myDebugPrint3("BarrierLock A " + str(BarrierLockCount) + " " + tagString + "\n") |
| 49 |
globalPidSumList = UseReduceOnIntegerList(localIntList, 2) |
| 50 |
if PhactoriDbg(100): |
| 51 |
myDebugPrint3("BarrierLock B " + str(BarrierLockCount) + " " + tagString + "\n") |
| 52 |
|
| 53 |
def UseReduceOnFloatList(inFloatList, inAllReduceType): |
| 54 |
"""inAllReduceType 0-max 1-min 2-sum""" |
| 55 |
#if PhactoriDbg(100): |
| 56 |
# myDebugPrint3("UseReduceOnFloatList entered " + str(inAllReduceType) + "\n", 100) |
| 57 |
|
| 58 |
#if PhactoriDbg(100): |
| 59 |
# myDebugPrint3("num values: " + str(len(inFloatList)) + "\n") |
| 60 |
# if len(inFloatList) > 0: |
| 61 |
# myDebugPrint3("local first: " + str(inFloatList[0]) + " last: " + str(inFloatList[-1]) + "\n") |
| 62 |
|
| 63 |
pm = paraview.servermanager.vtkProcessModule.GetProcessModule() |
| 64 |
globalController = pm.GetGlobalController() |
| 65 |
|
| 66 |
numVals = len(inFloatList) |
| 67 |
localarray = vtk.vtkDoubleArray() |
| 68 |
localarray.SetNumberOfTuples(numVals) |
| 69 |
globalarray = vtk.vtkDoubleArray() |
| 70 |
globalarray.SetNumberOfTuples(numVals) |
| 71 |
|
| 72 |
for idx, oneVal in enumerate(inFloatList): |
| 73 |
localarray.SetValue(idx, oneVal) |
| 74 |
|
| 75 |
globalController.AllReduce(localarray, globalarray, inAllReduceType) |
| 76 |
|
| 77 |
returnArray = [] |
| 78 |
for idx in range(0, numVals): |
| 79 |
returnArray.append(globalarray.GetTuple1(idx)) |
| 80 |
|
| 81 |
#if PhactoriDbg(100): |
| 82 |
# if len(returnArray) > 0: |
| 83 |
# myDebugPrint3("global first: " + str(returnArray[0]) + " last: " + str(returnArray[-1]) + "\n") |
| 84 |
|
| 85 |
#if PhactoriDbg(100): |
| 86 |
# myDebugPrint3("UseReduceOnFloatList returning\n", 100) |
| 87 |
return returnArray |
| 88 |
|
| 89 |
def UseReduceOnIntegerList(inIntegerList, inAllReduceType): |
| 90 |
#if PhactoriDbg(100): |
| 91 |
# myDebugPrint3("UseReduceOnIntegerList entered\n", 100) |
| 92 |
|
| 93 |
#if PhactoriDbg(100): |
| 94 |
# myDebugPrint3("num values: " + str(len(inIntegerList)) + "\n") |
| 95 |
# if len(inIntegerList) > 0: |
| 96 |
# myDebugPrint3("local first: " + str(inIntegerList[0]) + " last: " + str(inIntegerList[-1]) + "\n") |
| 97 |
|
| 98 |
pm = paraview.servermanager.vtkProcessModule.GetProcessModule() |
| 99 |
globalController = pm.GetGlobalController() |
| 100 |
|
| 101 |
numVals = len(inIntegerList) |
| 102 |
localarray = vtk.vtkIntArray() |
| 103 |
localarray.SetNumberOfTuples(numVals) |
| 104 |
globalarray = vtk.vtkIntArray() |
| 105 |
globalarray.SetNumberOfTuples(numVals) |
| 106 |
|
| 107 |
for idx, oneVal in enumerate(inIntegerList): |
| 108 |
localarray.SetValue(idx, oneVal) |
| 109 |
|
| 110 |
globalController.AllReduce(localarray, globalarray, inAllReduceType) |
| 111 |
|
| 112 |
returnArray = [] |
| 113 |
for idx in range(0, numVals): |
| 114 |
returnArray.append(int(globalarray.GetTuple1(idx))) |
| 115 |
|
| 116 |
#if PhactoriDbg(100): |
| 117 |
# if len(returnArray) > 0: |
| 118 |
# myDebugPrint3("global first: " + str(returnArray[0]) + " last: " + str(returnArray[-1]) + "\n") |
| 119 |
|
| 120 |
#if PhactoriDbg(100): |
| 121 |
# myDebugPrint3("UseReduceOnIntegerList returning\n", 100) |
| 122 |
return returnArray |
| 123 |
|
| 124 |
def UseMpiToShareAllProcessArraySizes(inArray): |
| 125 |
if PhactoriDbg(100): |
| 126 |
myDebugPrint3("UseMpiToShareAllProcessArraySizes entered\n", 100) |
| 127 |
numProcs = SmartGetNumberOfProcesses() |
| 128 |
myPid = SmartGetLocalProcessId() |
| 129 |
|
| 130 |
localArraySize = [] |
| 131 |
for ii in range(0, numProcs): |
| 132 |
localArraySize.append(int(0)) |
| 133 |
|
| 134 |
localArraySize[myPid] = int(len(inArray)) |
| 135 |
globalArraySize = UseReduceOnIntegerList(localArraySize, 0) |
| 136 |
if PhactoriDbg(100): |
| 137 |
myDebugPrint3(str(localArraySize) + "\n") |
| 138 |
myDebugPrint3(str(globalArraySize) + "\n") |
| 139 |
myDebugPrint3("UseMpiToShareAllProcessArraySizes returning\n", 100) |
| 140 |
return globalArraySize |
| 141 |
|
| 142 |
def UseMpiToSendAllProcessesFloatArrayToOneProcess(thisProcessFloatArray, pidToCollectArray): |
| 143 |
"""given a float array on this process, send the float arrays from all |
| 144 |
processes to the process pidToCollectArray. Gets sizes from all |
| 145 |
processes first and then uses reduce to send it all""" |
| 146 |
|
| 147 |
if PhactoriDbg(100): |
| 148 |
myDebugPrint3("UseMpiToSendAllProcessesFloatArrayToOneProcess entered\n", 100) |
| 149 |
|
| 150 |
numProcs = SmartGetNumberOfProcesses() |
| 151 |
myPid = SmartGetLocalProcessId() |
| 152 |
|
| 153 |
globalArraySize = UseMpiToShareAllProcessArraySizes(thisProcessFloatArray) |
| 154 |
|
| 155 |
localFloatArray = [] |
| 156 |
for pidIndex, oneSize in enumerate(globalArraySize): |
| 157 |
if pidIndex == myPid: |
| 158 |
for vv in thisProcessFloatArray: |
| 159 |
localFloatArray.append(vv) |
| 160 |
else: |
| 161 |
for cnt1 in range(0, oneSize): |
| 162 |
localFloatArray.append(0.0) |
| 163 |
|
| 164 |
#we really don't need to use AllReduce here, just Reduce to one process |
| 165 |
#fix later |
| 166 |
globalFloatArray = UseReduceOnFloatList(localFloatArray, 2) |
| 167 |
|
| 168 |
#we really should only have value for pidToCollectArray |
| 169 |
if myPid == pidToCollectArray: |
| 170 |
retVal = globalFloatArray |
| 171 |
else: |
| 172 |
retVal = [] |
| 173 |
|
| 174 |
if PhactoriDbg(100): |
| 175 |
if len(globalFloatArray) == 0: |
| 176 |
myDebugPrint3(str(len(globalFloatArray)) + "\n") |
| 177 |
else: |
| 178 |
myDebugPrint3(str(len(globalFloatArray)) + " " + str(globalFloatArray[0]) + " " + str(globalFloatArray[-1]) + "\n") |
| 179 |
myDebugPrint3("UseMpiToSendAllProcessesFloatArrayToOneProcess returning\n", 100) |
| 180 |
return retVal |
| 181 |
|
| 182 |
def UseMpiToSendAllProcessesIntArrayToOneProcess(thisProcessIntArray, pidToCollectArray): |
| 183 |
"""given a int array on this process, send the int arrays from all |
| 184 |
processes to the process pidToCollectArray. Gets sizes from all |
| 185 |
processes first and then uses reduce to send it all""" |
| 186 |
if PhactoriDbg(100): |
| 187 |
myDebugPrint3("UseMpiToSendAllProcessesintArrayToOneProcess entered\n", 100) |
| 188 |
numProcs = SmartGetNumberOfProcesses() |
| 189 |
myPid = SmartGetLocalProcessId() |
| 190 |
|
| 191 |
globalArraySize = UseMpiToShareAllProcessArraySizes(thisProcessIntArray) |
| 192 |
|
| 193 |
localIntArray = [] |
| 194 |
for pidIndex, oneSize in enumerate(globalArraySize): |
| 195 |
if pidIndex == myPid: |
| 196 |
for vv in thisProcessIntArray: |
| 197 |
localIntArray.append(vv) |
| 198 |
else: |
| 199 |
for cnt1 in range(0, oneSize): |
| 200 |
localIntArray.append(int(0)) |
| 201 |
|
| 202 |
#we really don't need to use AllReduce here, just Reduce to one process |
| 203 |
#fix later |
| 204 |
globalIntArray = UseReduceOnIntegerList(localIntArray, 2) |
| 205 |
|
| 206 |
#we really should only have value for pidToCollectArray |
| 207 |
if myPid == pidToCollectArray: |
| 208 |
retVal = globalIntArray |
| 209 |
else: |
| 210 |
retVal = [] |
| 211 |
|
| 212 |
if PhactoriDbg(100): |
| 213 |
if len(globalIntArray) == 0: |
| 214 |
myDebugPrint3(str(len(globalIntArray)) + "\n") |
| 215 |
else: |
| 216 |
myDebugPrint3(str(len(globalIntArray)) + " " + str(globalIntArray[0]) + " " + str(globalIntArray[-1]) + "\n") |
| 217 |
myDebugPrint3("UseMpiToSendAllProcessesintArrayToOneProcess returning\n", 100) |
| 218 |
return retVal |
| 219 |
|
| 220 |
def ReadAndMpiBroadcastJsonFile(inJsonFileName): |
| 221 |
"""given a json format file, read in the contents using the python json |
| 222 |
library on process zero, use MPI to broadcast the contents to all |
| 223 |
processes (via converting to a long char string)""" |
| 224 |
if SmartGetLocalProcessId() == 0: |
| 225 |
#process zero loads json script and broadcasts |
| 226 |
returnJson = HandleJsonScriptLoadProcessZero(inJsonFileName) |
| 227 |
else: |
| 228 |
#other processes receive json script |
| 229 |
returnJson = HandleJsonScriptLoadProcessNotZero() |
| 230 |
return returnJson |
| 231 |
|
| 232 |
#phactori_combine_to_single_python_file_subpiece_end_1 |
| 233 |
|