MCPcopy Create free account
hub / github.com/CVCUDA/CV-CUDA / SetTensorToRandomValue

Function SetTensorToRandomValue

tests/common/TensorDataUtils.hpp:451–487  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

449
450template<typename DT>
451static void SetTensorToRandomValue(const TensorData &tensorData, DT minVal, DT maxVal, int sample)
452{
453 if (!nvcv::TensorDataAccessStrided::IsCompatible(tensorData))
454 throw std::runtime_error("Tensor Data is not pitch access capable.");
455
456 auto tDataAc = nvcv::TensorDataAccessStrided::Create(tensorData);
457
458 if (tDataAc->numSamples() <= sample)
459 throw std::runtime_error("Number of samples smaller than requested sample.");
460
461 int inElements = (tDataAc->sampleStride() / sizeof(DT));
462 std::vector<DT> srcVec(inElements);
463 std::default_random_engine randEng(0);
464
465 int totalSamples;
466 if (sample < 0)
467 {
468 totalSamples = tDataAc->numSamples();
469 sample = 0;
470 }
471 else
472 {
473 totalSamples = sample + 1;
474 }
475 std::uniform_int_distribution<DT> srcRand{minVal, maxVal};
476 for (int i = sample; i < totalSamples; ++i)
477 {
478 std::generate(srcVec.begin(), srcVec.end(), [&]() { return srcRand(randEng); });
479 if (cudaSuccess
480 != cudaMemcpy(tDataAc->sampleData(i), srcVec.data(), tDataAc->sampleStride(), cudaMemcpyHostToDevice))
481 {
482 throw std::runtime_error("CudaMemcpy failed");
483 }
484 }
485
486 return;
487}
488
489template<typename DT>
490void SetTensorFromVector(const TensorData &tensorData, std::vector<DT> &data, int sample)

Callers

nothing calls this directly

Calls 6

sampleStrideMethod · 0.80
numSamplesMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
sampleDataMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected