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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* ************************************************************************
* Copyright (C) 2020 Advanced Micro Devices, Inc. All rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ************************************************************************ */
#pragma once
#ifndef TESTING_SPVV_HPP
#define TESTING_SPVV_HPP
#include "display.hpp"
#include "flops.hpp"
#include "gbyte.hpp"
#include "hipsparse_arguments.hpp"
#include "hipsparse_test_unique_ptr.hpp"
#include "unit.hpp"
#include "utility.hpp"
#include <hipsparse.h>
#include <typeinfo>
using namespace hipsparse_test;
void testing_spvv_bad_arg(void)
{
#if(!defined(CUDART_VERSION) || CUDART_VERSION > 10010 \
|| (CUDART_VERSION == 10010 && CUDART_10_1_UPDATE_VERSION == 1))
int64_t size = 100;
int64_t nnz = 100;
float result;
hipsparseOperation_t opType = HIPSPARSE_OPERATION_NON_TRANSPOSE;
hipsparseIndexType_t idxType = HIPSPARSE_INDEX_32I;
hipsparseIndexBase_t idxBase = HIPSPARSE_INDEX_BASE_ZERO;
hipDataType dataType = HIP_R_32F;
std::unique_ptr<handle_struct> unique_ptr_handle(new handle_struct);
hipsparseHandle_t handle = unique_ptr_handle->handle;
auto dx_val_managed = hipsparse_unique_ptr{device_malloc(sizeof(float) * nnz), device_free};
auto dx_ind_managed = hipsparse_unique_ptr{device_malloc(sizeof(int) * nnz), device_free};
auto dy_managed = hipsparse_unique_ptr{device_malloc(sizeof(float) * size), device_free};
float* dx_val = (float*)dx_val_managed.get();
int* dx_ind = (int*)dx_ind_managed.get();
float* dy = (float*)dy_managed.get();
// Structures
hipsparseSpVecDescr_t x;
hipsparseDnVecDescr_t y;
verify_hipsparse_status_success(
hipsparseCreateSpVec(&x, size, nnz, dx_ind, dx_val, idxType, idxBase, dataType), "Success");
verify_hipsparse_status_success(hipsparseCreateDnVec(&y, size, dy, dataType), "Success");
// SpVV bufferSize
size_t bufferSize;
verify_hipsparse_status_invalid_handle(
hipsparseSpVV_bufferSize(nullptr, opType, x, y, &result, dataType, &bufferSize));
verify_hipsparse_status_invalid_pointer(
hipsparseSpVV_bufferSize(handle, opType, nullptr, y, &result, dataType, &bufferSize),
"Error: x is nullptr");
verify_hipsparse_status_invalid_pointer(
hipsparseSpVV_bufferSize(handle, opType, x, nullptr, &result, dataType, &bufferSize),
"Error: y is nullptr");
verify_hipsparse_status_invalid_pointer(
hipsparseSpVV_bufferSize(handle, opType, x, y, nullptr, dataType, &bufferSize),
"Error: result is nullptr");
verify_hipsparse_status_invalid_pointer(
hipsparseSpVV_bufferSize(handle, opType, x, y, &result, dataType, nullptr),
"Error: bufferSize is nullptr");
// SpVV
void* buffer;
CHECK_HIP_ERROR(hipMalloc(&buffer, 100));
verify_hipsparse_status_invalid_handle(
hipsparseSpVV(nullptr, opType, x, y, &result, dataType, buffer));
verify_hipsparse_status_invalid_pointer(
hipsparseSpVV(handle, opType, nullptr, y, &result, dataType, buffer),
"Error: x is nullptr");
verify_hipsparse_status_invalid_pointer(
hipsparseSpVV(handle, opType, x, nullptr, &result, dataType, buffer),
"Error: y is nullptr");
verify_hipsparse_status_invalid_pointer(
hipsparseSpVV(handle, opType, x, y, nullptr, dataType, buffer), "Error: result is nullptr");
verify_hipsparse_status_invalid_pointer(
hipsparseSpVV(handle, opType, x, y, &result, dataType, nullptr),
"Error: buffer is nullptr");
// Destruct
verify_hipsparse_status_success(hipsparseDestroySpVec(x), "Success");
verify_hipsparse_status_success(hipsparseDestroyDnVec(y), "Success");
CHECK_HIP_ERROR(hipFree(buffer));
#endif
}
template <typename I, typename T>
hipsparseStatus_t testing_spvv(Arguments argus)
{
#if(!defined(CUDART_VERSION) || CUDART_VERSION > 10010 \
|| (CUDART_VERSION == 10010 && CUDART_10_1_UPDATE_VERSION == 1))
I size = argus.N;
I nnz = argus.nnz;
hipsparseOperation_t trans = argus.transA;
hipsparseIndexBase_t idxBase = argus.baseA;
// Index and data type
hipsparseIndexType_t idxType = getIndexType<I>();
hipDataType dataType = getDataType<T>();
// hipSPARSE handle
std::unique_ptr<handle_struct> unique_ptr_handle(new handle_struct);
hipsparseHandle_t handle = unique_ptr_handle->handle;
hipStream_t stream;
CHECK_HIPSPARSE_ERROR(hipsparseGetStream(handle, &stream));
// Host structures
std::vector<I> hx_ind(nnz);
std::vector<T> hx_val(nnz);
std::vector<T> hy(size);
// Initial Data on CPU
srand(12345ULL);
hipsparseInitIndex(hx_ind.data(), nnz, 1, size);
hipsparseInit<T>(hx_val, 1, nnz);
hipsparseInit<T>(hy, 1, size);
// Allocate memory on device
auto dx_ind_managed = hipsparse_unique_ptr{device_malloc(sizeof(I) * nnz), device_free};
auto dx_val_managed = hipsparse_unique_ptr{device_malloc(sizeof(T) * nnz), device_free};
auto dy_managed = hipsparse_unique_ptr{device_malloc(sizeof(T) * size), device_free};
auto dresult_managed = hipsparse_unique_ptr{device_malloc(sizeof(T)), device_free};
I* dx_ind = (I*)dx_ind_managed.get();
T* dx_val = (T*)dx_val_managed.get();
T* dy = (T*)dy_managed.get();
T* dresult = (T*)dresult_managed.get();
// copy data from CPU to device
CHECK_HIP_ERROR(hipMemcpy(dx_ind, hx_ind.data(), sizeof(I) * nnz, hipMemcpyHostToDevice));
CHECK_HIP_ERROR(hipMemcpy(dx_val, hx_val.data(), sizeof(T) * nnz, hipMemcpyHostToDevice));
CHECK_HIP_ERROR(hipMemcpy(dy, hy.data(), sizeof(T) * size, hipMemcpyHostToDevice));
// Create structures
hipsparseSpVecDescr_t x;
hipsparseDnVecDescr_t y;
CHECK_HIPSPARSE_ERROR(
hipsparseCreateSpVec(&x, size, nnz, dx_ind, dx_val, idxType, idxBase, dataType));
CHECK_HIPSPARSE_ERROR(hipsparseCreateDnVec(&y, size, dy, dataType));
T hresult;
T hresult_gold;
T hresult_copied_from_device;
// SpVV_bufferSize
size_t bufferSize;
CHECK_HIPSPARSE_ERROR(
hipsparseSpVV_bufferSize(handle, trans, x, y, &hresult, dataType, &bufferSize));
void* externalBuffer;
CHECK_HIP_ERROR(hipMalloc(&externalBuffer, bufferSize));
if(argus.unit_check)
{
CHECK_HIPSPARSE_ERROR(hipsparseSetPointerMode(handle, HIPSPARSE_POINTER_MODE_HOST));
CHECK_HIPSPARSE_ERROR(
hipsparseSpVV(handle, trans, x, y, &hresult, dataType, externalBuffer));
CHECK_HIPSPARSE_ERROR(hipsparseSetPointerMode(handle, HIPSPARSE_POINTER_MODE_DEVICE));
CHECK_HIPSPARSE_ERROR(
hipsparseSpVV(handle, trans, x, y, dresult, dataType, externalBuffer));
// Copy output from device to CPU
CHECK_HIP_ERROR(
hipMemcpy(&hresult_copied_from_device, dresult, sizeof(T), hipMemcpyDeviceToHost));
// CPU solution
if(trans == HIPSPARSE_OPERATION_CONJUGATE_TRANSPOSE)
{
hresult_gold = make_DataType<T>(0);
for(I i = 0; i < nnz; ++i)
{
hresult_gold
= hresult_gold + testing_mult(testing_conj(hx_val[i]), hy[hx_ind[i] - idxBase]);
}
}
else
{
hresult_gold = make_DataType<T>(0);
for(I i = 0; i < nnz; ++i)
{
hresult_gold = hresult_gold + testing_mult(hx_val[i], hy[hx_ind[i] - idxBase]);
}
}
// Verify results against host
unit_check_general(1, 1, 1, &hresult_gold, &hresult);
unit_check_general(1, 1, 1, &hresult_gold, &hresult_copied_from_device);
}
if(argus.timing)
{
int number_cold_calls = 2;
int number_hot_calls = argus.iters;
CHECK_HIPSPARSE_ERROR(hipsparseSetPointerMode(handle, HIPSPARSE_POINTER_MODE_HOST));
// Warm up
for(int iter = 0; iter < number_cold_calls; ++iter)
{
CHECK_HIPSPARSE_ERROR(
hipsparseSpVV(handle, trans, x, y, &hresult, dataType, externalBuffer));
CHECK_HIP_ERROR(hipStreamSynchronize(stream));
}
double gpu_time_used = get_time_us();
// Performance run
for(int iter = 0; iter < number_hot_calls; ++iter)
{
CHECK_HIPSPARSE_ERROR(
hipsparseSpVV(handle, trans, x, y, &hresult, dataType, externalBuffer));
CHECK_HIP_ERROR(hipStreamSynchronize(stream));
}
gpu_time_used = (get_time_us() - gpu_time_used) / number_hot_calls;
double gflop_count = doti_gflop_count(nnz);
double gbyte_count = doti_gbyte_count<T, T>(nnz);
double gpu_gbyte = get_gpu_gbyte(gpu_time_used, gbyte_count);
double gpu_gflops = get_gpu_gflops(gpu_time_used, gflop_count);
display_timing_info(display_key_t::nnz,
nnz,
display_key_t::gflops,
gpu_gflops,
display_key_t::bandwidth,
gpu_gbyte,
display_key_t::time_ms,
get_gpu_time_msec(gpu_time_used));
}
CHECK_HIP_ERROR(hipFree(externalBuffer));
CHECK_HIPSPARSE_ERROR(hipsparseDestroySpVec(x));
CHECK_HIPSPARSE_ERROR(hipsparseDestroyDnVec(y));
#endif
return HIPSPARSE_STATUS_SUCCESS;
}
#endif // TESTING_SPVV_HPP
|