Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
velocity_mesh_parameters.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2024 Finnish Meteorological Institute and University of Helsinki
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
24#include <iostream>
25#include <cstdlib>
26
27#ifdef USE_GPU
28 #include "include/splitvector/splitvec.h"
29 #include "arch/gpu_base.hpp"
30#endif
31
32// Pointers to MeshWrapper objects
34
35#ifdef USE_GPU
37std::array<vmesh::MeshParameters,MAX_VMESH_PARAMETERS_COUNT> *velocityMeshes_upload;
38
39__global__ void debug_kernel(const uint popID) {
41}
42#endif
43
47
51
52#ifdef USE_GPU
53//#pragma hd_warning_disable // only applies to next function
54#pragma nv_diag_suppress=20091
55// We record the set of constant memory symbols to static arrays. We are avoiding using objects with non-trivial Ctors
56// to not risk they to be constructed after the static object that uses them. There will be an instance of the static
57// objects per compilation unit, so the ordering is hard to control.
58static vmesh::MeshWrapper** meshWrapperDevRegister[128] = {0};
59vmesh::meshWrapperDevRegistor::meshWrapperDevRegistor(vmesh::MeshWrapper*& v) {
60 for (size_t InstanceIdx = 0; InstanceIdx < sizeof(meshWrapperDevRegister) / sizeof(vmesh::MeshWrapper**);
61 ++InstanceIdx) {
62 if (auto*& slot = meshWrapperDevRegister[InstanceIdx]; !slot) {
63 slot = &v;
64 //printf("Got instance of device mesh wrapper handler %p (index %ld)\n", &v, InstanceIdx);
65 return;
66 }
67 }
68 assert(false && "Not enough slots to register mesh wrapper slots.");
69}
70
72 // Store address to velocityMeshes array
73 std::array<vmesh::MeshParameters,MAX_VMESH_PARAMETERS_COUNT> * temp = meshWrapper->velocityMeshes;
74 // gpu-Malloc space on device, copy array contents
75 CHK_ERR( gpuMalloc((void **)&velocityMeshes_upload, sizeof(std::array<vmesh::MeshParameters,MAX_VMESH_PARAMETERS_COUNT>)) );
76 CHK_ERR( gpuMemcpy(velocityMeshes_upload, meshWrapper->velocityMeshes, sizeof(std::array<vmesh::MeshParameters,MAX_VMESH_PARAMETERS_COUNT>),gpuMemcpyHostToDevice) );
77 // Make wrapper point to device-side array
78 meshWrapper->velocityMeshes = velocityMeshes_upload;
79 // Allocate and copy meshwrapper on device
80 CHK_ERR( gpuMalloc((void **)&MWdev, sizeof(vmesh::MeshWrapper)) );
81 CHK_ERR( gpuMemcpy(MWdev, meshWrapper, sizeof(vmesh::MeshWrapper),gpuMemcpyHostToDevice) );
82 // Set the global symbol of meshWrapper
83 int count=0;
84 for (size_t InstanceIdx = 0; InstanceIdx < sizeof(meshWrapperDevRegister) / sizeof(vmesh::MeshWrapper**);
85 ++InstanceIdx) {
86 if (auto* slot = meshWrapperDevRegister[InstanceIdx]; slot) {
87 //printf("Setting device mesh wrapper handler %p (index %ld)\n", slot, InstanceIdx);
88 CHK_ERR( gpuMemcpyToSymbol(*slot, &MWdev, sizeof(vmesh::MeshWrapper*)) );
89 count++;
90 } else {
91 break;
92 }
93 }
94 int myRank;
95 MPI_Comm_rank(MPI_COMM_WORLD,&myRank);
96 if(myRank == MASTER_RANK) {
97 printf("Done setting all %d instances of device mesh wrapper handler!\n",count);
98 }
99
100 // Copy host-side address back
102 // And sync
104}
105void vmesh::deallocateMeshWrapper() {
106 CHK_ERR( gpuFree(velocityMeshes_upload) );
107 CHK_ERR( gpuFree(MWdev) );
108 CHK_ERR( gpuFree(meshWrapperDevInstance) );
109 // And sync
111}
112#endif
113
115 // Verify lengths match?
116 if (meshWrapper->velocityMeshesCreation->size() != nMeshes) {
117 printf("Error! Initialized only %d velocity meshes out of %d created ones.\n",nMeshes,
118 (int)meshWrapper->velocityMeshesCreation->size());
119 abort();
120 }
121 // Create pointer to array of sufficient length
122 meshWrapper->velocityMeshes = new std::array<vmesh::MeshParameters,MAX_VMESH_PARAMETERS_COUNT>;
123
124 // Copy data in, also set auxiliary values
125 for (uint i=0; i<nMeshes; ++i) {
126 vmesh::MeshParameters* vMesh = &(meshWrapper->velocityMeshes->at(i));
127 const vmesh::MeshParameters* vMeshIn = &(meshWrapper->velocityMeshesCreation->at(i));
128
129 // Limits
130 vMesh->meshLimits[0] = vMeshIn->meshLimits[0];
131 vMesh->meshLimits[1] = vMeshIn->meshLimits[1];
132 vMesh->meshLimits[2] = vMeshIn->meshLimits[2];
133 vMesh->meshLimits[3] = vMeshIn->meshLimits[3];
134 vMesh->meshLimits[4] = vMeshIn->meshLimits[4];
135 vMesh->meshLimits[5] = vMeshIn->meshLimits[5];
136 // Grid length
137 vMesh->gridLength[0] = vMeshIn->gridLength[0];
138 vMesh->gridLength[1] = vMeshIn->gridLength[1];
139 vMesh->gridLength[2] = vMeshIn->gridLength[2];
140 // Block length
141 vMesh->blockLength[0] = vMeshIn->blockLength[0];
142 vMesh->blockLength[1] = vMeshIn->blockLength[1];
143 vMesh->blockLength[2] = vMeshIn->blockLength[2];
144
145 // Calculate derived mesh parameters:
146 vMesh->meshMinLimits[0] = vMesh->meshLimits[0];
147 vMesh->meshMinLimits[1] = vMesh->meshLimits[2];
148 vMesh->meshMinLimits[2] = vMesh->meshLimits[4];
149 vMesh->meshMaxLimits[0] = vMesh->meshLimits[1];
150 vMesh->meshMaxLimits[1] = vMesh->meshLimits[3];
151 vMesh->meshMaxLimits[2] = vMesh->meshLimits[5];
152
153 vMesh->gridSize[0] = vMesh->meshMaxLimits[0] - vMesh->meshMinLimits[0];
154 vMesh->gridSize[1] = vMesh->meshMaxLimits[1] - vMesh->meshMinLimits[1];
155 vMesh->gridSize[2] = vMesh->meshMaxLimits[2] - vMesh->meshMinLimits[2];
156
157 vMesh->blockSize[0] = vMesh->gridSize[0] / vMesh->gridLength[0];
158 vMesh->blockSize[1] = vMesh->gridSize[1] / vMesh->gridLength[1];
159 vMesh->blockSize[2] = vMesh->gridSize[2] / vMesh->gridLength[2];
160
161 vMesh->cellSize[0] = vMesh->blockSize[0] / vMesh->blockLength[0];
162 vMesh->cellSize[1] = vMesh->blockSize[1] / vMesh->blockLength[1];
163 vMesh->cellSize[2] = vMesh->blockSize[2] / vMesh->blockLength[2];
164
166 = vMeshIn->gridLength[0]
167 * vMeshIn->gridLength[1]
168 * vMeshIn->gridLength[2];
169 vMesh->initialized = true;
170 }
171#ifdef USE_GPU
172 // Now all velocity meshes have been initialized on host, into
173 // the array. Now we need to upload a copy onto GPU.
175 // printf("Host printout\n");
176 // vmesh::printVelocityMesh(0);
177 // printf("Device printout\n");
178 // debug_kernel<<<1, 1, 0, 0>>> (0);
179 // CHK_ERR( gpuDeviceSynchronize() );
180#endif
181
182 return;
183}
for i
Definition Dispersion.m:24
#define gpuMalloc
#define gpuMemcpyHostToDevice
#define CHK_ERR(err)
#define gpuMemcpy
#define gpuMemcpyToSymbol
#define gpuFree
#define gpuDeviceSynchronize
#define MASTER_RANK
Definition common.h:67
int myRank
Definition gpu_base.cpp:48
ARCH_HOSTDEV void printVelocityMesh(const uint meshIndex)
MeshWrapper * host_getMeshWrapper()
void initVelocityMeshes(const uint nMeshes)
std::array< vmesh::MeshParameters, MAX_VMESH_PARAMETERS_COUNT > * velocityMeshes
static vmesh::MeshWrapper * meshWrapper