|
| 1 | +#include <cassert> |
| 2 | +#include <csignal> |
| 3 | +#include <sstream> |
| 4 | + |
| 5 | +#include "scuda.h" |
| 6 | +#include "squad.h" |
| 7 | + |
| 8 | +#include "NP.hh" |
| 9 | +#include "SLOG.hh" |
| 10 | +#include "ssys.h" |
| 11 | + |
| 12 | +#include "QTex.hh" |
| 13 | +#include "QU.hh" |
| 14 | +#include "QUDA_CHECK.h" |
| 15 | +#include "QWls.hh" |
| 16 | + |
| 17 | +#include "qwls.h" |
| 18 | + |
| 19 | +const plog::Severity QWls::LEVEL = SLOG::EnvLevel("QWls", "DEBUG"); |
| 20 | + |
| 21 | +const QWls *QWls::INSTANCE = nullptr; |
| 22 | +const QWls *QWls::Get() |
| 23 | +{ |
| 24 | + return INSTANCE; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | +QWls::QWls |
| 29 | +------------ |
| 30 | +
|
| 31 | +1. Narrows ICDF from double to float if needed |
| 32 | +2. Uploads ICDF into GPU texture |
| 33 | +3. Creates qwls instance with device pointers and uploads it |
| 34 | +
|
| 35 | +**/ |
| 36 | + |
| 37 | +QWls::QWls(const NP *wls_icdf, const NP *mat_map, const NP *time_constants, unsigned hd_factor) : |
| 38 | + dsrc(wls_icdf->ebyte == 8 ? wls_icdf : nullptr), |
| 39 | + src(wls_icdf->ebyte == 4 ? wls_icdf : NP::MakeNarrow(dsrc)), |
| 40 | + tex(MakeWlsTex(src, hd_factor)), |
| 41 | + wls(MakeInstance(tex, mat_map, time_constants, hd_factor, time_constants->shape[0])), |
| 42 | + d_wls(QU::UploadArray<qwls>(wls, 1, "QWls::QWls/d_wls")) |
| 43 | +{ |
| 44 | + INSTANCE = this; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | +QWls::MakeWlsTex |
| 49 | +------------------- |
| 50 | +
|
| 51 | +Creates a 2D CUDA texture from the ICDF array. |
| 52 | +Shape: (num_wls*3, 4096, 1) where 3 = HD layers per material. |
| 53 | +
|
| 54 | +**/ |
| 55 | + |
| 56 | +QTex<float> *QWls::MakeWlsTex(const NP *src, unsigned hd_factor) |
| 57 | +{ |
| 58 | + assert(src); |
| 59 | + assert(src->shape.size() == 3); |
| 60 | + |
| 61 | + unsigned ni = src->shape[0]; // height: num_wls * 3 |
| 62 | + unsigned nj = src->shape[1]; // width: 4096 |
| 63 | + unsigned nk = src->shape[2]; // 1 |
| 64 | + |
| 65 | + assert(nk == 1); |
| 66 | + assert(nj == 4096); |
| 67 | + assert(ni % 3 == 0); // must be multiple of 3 (3 HD layers per material) |
| 68 | + assert(src->uifc == 'f' && src->ebyte == 4); |
| 69 | + |
| 70 | + unsigned ny = ni; // height |
| 71 | + unsigned nx = nj; // width |
| 72 | + |
| 73 | + bool normalizedCoords = true; |
| 74 | + QTex<float> *tx = new QTex<float>(nx, ny, src->cvalues<float>(), 'L', normalizedCoords, src); |
| 75 | + |
| 76 | + tx->setHDFactor(hd_factor); |
| 77 | + tx->uploadMeta(); |
| 78 | + |
| 79 | + LOG(LEVEL) << " src " << src->desc() << " nx (width) " << nx << " ny (height) " << ny << " tx.HDFactor " |
| 80 | + << tx->getHDFactor(); |
| 81 | + |
| 82 | + return tx; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | +QWls::MakeInstance |
| 87 | +--------------------- |
| 88 | +
|
| 89 | +Creates the host-side qwls struct populated with device pointers. |
| 90 | +Uploads material_map and time_constants to device memory. |
| 91 | +
|
| 92 | +**/ |
| 93 | + |
| 94 | +qwls *QWls::MakeInstance(const QTex<float> *tex, const NP *mat_map, const NP *time_constants, unsigned hd_factor, |
| 95 | + unsigned num_wls) |
| 96 | +{ |
| 97 | + assert(mat_map); |
| 98 | + assert(time_constants); |
| 99 | + assert(mat_map->uifc == 'i' && mat_map->ebyte == 4); |
| 100 | + assert(time_constants->uifc == 'f' && time_constants->ebyte == 4); |
| 101 | + |
| 102 | + qwls *w = new qwls; |
| 103 | + w->wls_tex = tex->texObj; |
| 104 | + w->hd_factor = hd_factor; |
| 105 | + w->num_wls = num_wls; |
| 106 | + w->tex_height = tex->height; |
| 107 | + |
| 108 | + // Upload material_map to device |
| 109 | + unsigned num_mat = mat_map->shape[0]; |
| 110 | + int *d_mat_map = nullptr; |
| 111 | + size_t mat_map_size = num_mat * sizeof(int); |
| 112 | + QUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&d_mat_map), mat_map_size)); |
| 113 | + QUDA_CHECK(cudaMemcpy(d_mat_map, mat_map->cvalues<int>(), mat_map_size, cudaMemcpyHostToDevice)); |
| 114 | + w->material_map = d_mat_map; |
| 115 | + |
| 116 | + // Upload time_constants to device |
| 117 | + float *d_tc = nullptr; |
| 118 | + size_t tc_size = num_wls * sizeof(float); |
| 119 | + QUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&d_tc), tc_size)); |
| 120 | + QUDA_CHECK(cudaMemcpy(d_tc, time_constants->cvalues<float>(), tc_size, cudaMemcpyHostToDevice)); |
| 121 | + w->time_constants = d_tc; |
| 122 | + |
| 123 | + return w; |
| 124 | +} |
| 125 | + |
| 126 | +std::string QWls::desc() const |
| 127 | +{ |
| 128 | + std::stringstream ss; |
| 129 | + ss << "QWls" << " dsrc " << (dsrc ? dsrc->desc() : "-") << " src " << (src ? src->desc() : "-") << " tex " |
| 130 | + << (tex ? tex->desc() : "-"); |
| 131 | + return ss.str(); |
| 132 | +} |
0 commit comments