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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
|
/* ************************************************************************
* 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_PRUNE_DENSE2CSR_HPP
#define TESTING_PRUNE_DENSE2CSR_HPP
#include "display.hpp"
#include "flops.hpp"
#include "gbyte.hpp"
#include "hipsparse.hpp"
#include "hipsparse_arguments.hpp"
#include "hipsparse_test_unique_ptr.hpp"
#include "unit.hpp"
#include "utility.hpp"
#include <algorithm>
#include <hipsparse.h>
#include <string>
using namespace hipsparse;
using namespace hipsparse_test;
template <typename T>
void testing_prune_dense2csr_bad_arg(void)
{
#if(!defined(CUDART_VERSION))
size_t safe_size = 100;
int M = 1;
int N = 1;
int LDA = M;
T threshold = static_cast<T>(1);
int nnz_total_dev_host_ptr = 100;
size_t buffer_size = 100;
hipsparseStatus_t status;
std::unique_ptr<handle_struct> unique_ptr_handle(new handle_struct);
hipsparseHandle_t handle = unique_ptr_handle->handle;
std::unique_ptr<descr_struct> unique_ptr_descr(new descr_struct);
hipsparseMatDescr_t descr = unique_ptr_descr->descr;
auto csr_row_ptr_managed
= hipsparse_unique_ptr{device_malloc(sizeof(int) * (safe_size + 1)), device_free};
auto csr_col_ind_managed
= hipsparse_unique_ptr{device_malloc(sizeof(int) * safe_size), device_free};
auto csr_val_managed = hipsparse_unique_ptr{device_malloc(sizeof(T) * safe_size), device_free};
auto A_managed = hipsparse_unique_ptr{device_malloc(sizeof(T) * safe_size), device_free};
auto temp_buffer_managed
= hipsparse_unique_ptr{device_malloc(sizeof(T) * safe_size), device_free};
int* csr_row_ptr = (int*)csr_row_ptr_managed.get();
int* csr_col_ind = (int*)csr_col_ind_managed.get();
T* csr_val = (T*)csr_val_managed.get();
T* A = (T*)A_managed.get();
T* temp_buffer = (T*)temp_buffer_managed.get();
int local_ptr[2] = {0, 1};
CHECK_HIP_ERROR(
hipMemcpy(csr_row_ptr, local_ptr, sizeof(int) * (1 + 1), hipMemcpyHostToDevice));
// Test hipsparseXpruneDense2csr_bufferSize
status = hipsparseXpruneDense2csr_bufferSize(
nullptr, M, N, A, LDA, &threshold, descr, csr_val, csr_row_ptr, csr_col_ind, &buffer_size);
verify_hipsparse_status_invalid_handle(status);
status = hipsparseXpruneDense2csr_bufferSize(
handle, M, N, A, LDA, &threshold, descr, csr_val, csr_row_ptr, csr_col_ind, nullptr);
verify_hipsparse_status_invalid_pointer(status, "Error: buffer size is nullptr");
// Test hipsparseXpruneDense2csrNnz
status = hipsparseXpruneDense2csrNnz(nullptr,
M,
N,
A,
LDA,
&threshold,
descr,
csr_row_ptr,
&nnz_total_dev_host_ptr,
temp_buffer);
verify_hipsparse_status_invalid_handle(status);
status = hipsparseXpruneDense2csrNnz(handle,
-1,
N,
A,
LDA,
&threshold,
descr,
csr_row_ptr,
&nnz_total_dev_host_ptr,
temp_buffer);
verify_hipsparse_status_invalid_size(status, "Error: M is invalid");
status = hipsparseXpruneDense2csrNnz(handle,
M,
-1,
A,
LDA,
&threshold,
descr,
csr_row_ptr,
&nnz_total_dev_host_ptr,
temp_buffer);
verify_hipsparse_status_invalid_size(status, "Error: N is invalid");
status = hipsparseXpruneDense2csrNnz(
handle, M, N, A, -1, &threshold, descr, csr_row_ptr, &nnz_total_dev_host_ptr, temp_buffer);
verify_hipsparse_status_invalid_size(status, "Error: LDA is invalid");
status = hipsparseXpruneDense2csrNnz(handle,
M,
N,
(const T*)nullptr,
LDA,
&threshold,
descr,
csr_row_ptr,
&nnz_total_dev_host_ptr,
temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: A is nullptr");
status = hipsparseXpruneDense2csrNnz(handle,
M,
N,
A,
LDA,
(const T*)nullptr,
descr,
csr_row_ptr,
&nnz_total_dev_host_ptr,
temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: threshold is nullptr");
status = hipsparseXpruneDense2csrNnz(handle,
M,
N,
A,
LDA,
&threshold,
nullptr,
csr_row_ptr,
&nnz_total_dev_host_ptr,
temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: descr is nullptr");
status = hipsparseXpruneDense2csrNnz(
handle, M, N, A, LDA, &threshold, descr, nullptr, &nnz_total_dev_host_ptr, temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: csr_row_ptr is nullptr");
status = hipsparseXpruneDense2csrNnz(
handle, M, N, A, LDA, &threshold, descr, csr_row_ptr, nullptr, temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: nnz_total_dev_host_ptr is nullptr");
// Test hipsparseXpruneDense2csr
status = hipsparseXpruneDense2csr(
nullptr, M, N, A, LDA, &threshold, descr, csr_val, csr_row_ptr, csr_col_ind, temp_buffer);
verify_hipsparse_status_invalid_handle(status);
status = hipsparseXpruneDense2csr(
handle, -1, N, A, LDA, &threshold, descr, csr_val, csr_row_ptr, csr_col_ind, temp_buffer);
verify_hipsparse_status_invalid_size(status, "Error: M is invalid");
status = hipsparseXpruneDense2csr(
handle, M, -1, A, LDA, &threshold, descr, csr_val, csr_row_ptr, csr_col_ind, temp_buffer);
verify_hipsparse_status_invalid_size(status, "Error: N is invalid");
status = hipsparseXpruneDense2csr(
handle, M, N, A, -1, &threshold, descr, csr_val, csr_row_ptr, csr_col_ind, temp_buffer);
verify_hipsparse_status_invalid_size(status, "Error: LDA is invalid");
status = hipsparseXpruneDense2csr(handle,
M,
N,
(const T*)nullptr,
LDA,
&threshold,
descr,
csr_val,
csr_row_ptr,
csr_col_ind,
temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: A is nullptr");
status = hipsparseXpruneDense2csr(handle,
M,
N,
A,
LDA,
(const T*)nullptr,
descr,
csr_val,
csr_row_ptr,
csr_col_ind,
temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: threshold is nullptr");
status = hipsparseXpruneDense2csr(
handle, M, N, A, LDA, &threshold, nullptr, csr_val, csr_row_ptr, csr_col_ind, temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: descr is nullptr");
status = hipsparseXpruneDense2csr(handle,
M,
N,
A,
LDA,
&threshold,
descr,
(T*)nullptr,
csr_row_ptr,
csr_col_ind,
temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: csr_val is nullptr");
status = hipsparseXpruneDense2csr(
handle, M, N, A, LDA, &threshold, descr, csr_val, nullptr, csr_col_ind, temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: csr_row_ptr is nullptr");
status = hipsparseXpruneDense2csr(
handle, M, N, A, LDA, &threshold, descr, csr_val, csr_row_ptr, nullptr, temp_buffer);
verify_hipsparse_status_invalid_pointer(status, "Error: csr_col_ind is nullptr");
#endif
}
template <typename T>
hipsparseStatus_t testing_prune_dense2csr(Arguments argus)
{
#if(!defined(CUDART_VERSION) || CUDART_VERSION < 13000)
int M = argus.M;
int N = argus.N;
int LDA = argus.lda;
T threshold = make_DataType<T>(argus.threshold);
hipsparseIndexBase_t idx_base = argus.baseA;
std::unique_ptr<handle_struct> unique_ptr_handle(new handle_struct);
hipsparseHandle_t handle = unique_ptr_handle->handle;
std::unique_ptr<descr_struct> unique_ptr_descr(new descr_struct);
hipsparseMatDescr_t descr = unique_ptr_descr->descr;
CHECK_HIPSPARSE_ERROR(hipsparseSetPointerMode(handle, HIPSPARSE_POINTER_MODE_HOST));
CHECK_HIPSPARSE_ERROR(hipsparseSetMatIndexBase(descr, idx_base));
if(M == 0 || N == 0)
{
#ifdef __HIP_PLATFORM_NVIDIA__
return HIPSPARSE_STATUS_SUCCESS;
#endif
}
// Allocate host memory
std::vector<T> h_A(LDA * N);
std::vector<int> h_nnz_total_dev_host_ptr(1);
// Allocate device memory
auto d_A_managed = hipsparse_unique_ptr{device_malloc(sizeof(T) * LDA * N), device_free};
auto d_nnz_total_dev_host_ptr_managed
= hipsparse_unique_ptr{device_malloc(sizeof(int)), device_free};
auto d_csr_row_ptr_managed
= hipsparse_unique_ptr{device_malloc(sizeof(int) * (M + 1)), device_free};
T* d_A = (T*)d_A_managed.get();
int* d_nnz_total_dev_host_ptr = (int*)d_nnz_total_dev_host_ptr_managed.get();
int* d_csr_row_ptr = (int*)d_csr_row_ptr_managed.get();
// Initialize the entire allocated memory.
for(int i = 0; i < LDA; ++i)
{
for(int j = 0; j < N; ++j)
{
h_A[j * LDA + i] = make_DataType<T>(-1);
}
}
// Initialize a random dense matrix.
srand(0);
gen_dense_random_sparsity_pattern(M, N, h_A.data(), LDA, HIPSPARSE_ORDER_COL, 0.2);
// Transfer.
CHECK_HIP_ERROR(hipMemcpy(d_A, h_A.data(), sizeof(T) * LDA * N, hipMemcpyHostToDevice));
size_t buffer_size = 512;
CHECK_HIPSPARSE_ERROR(hipsparseXpruneDense2csr_bufferSize(handle,
M,
N,
d_A,
LDA,
&threshold,
descr,
(const T*)nullptr,
d_csr_row_ptr,
(const int*)nullptr,
&buffer_size));
auto d_temp_buffer_managed = hipsparse_unique_ptr{device_malloc(buffer_size), device_free};
T* d_temp_buffer = (T*)d_temp_buffer_managed.get();
auto d_threshold_managed = hipsparse_unique_ptr{device_malloc(sizeof(T)), device_free};
T* d_threshold = (T*)d_threshold_managed.get();
CHECK_HIP_ERROR(hipMemcpy(d_threshold, &threshold, sizeof(T), hipMemcpyHostToDevice));
CHECK_HIPSPARSE_ERROR(hipsparseSetPointerMode(handle, HIPSPARSE_POINTER_MODE_HOST));
CHECK_HIPSPARSE_ERROR(hipsparseXpruneDense2csrNnz(handle,
M,
N,
d_A,
LDA,
&threshold,
descr,
d_csr_row_ptr,
&h_nnz_total_dev_host_ptr[0],
d_temp_buffer));
CHECK_HIPSPARSE_ERROR(hipsparseSetPointerMode(handle, HIPSPARSE_POINTER_MODE_DEVICE));
CHECK_HIPSPARSE_ERROR(hipsparseXpruneDense2csrNnz(handle,
M,
N,
d_A,
LDA,
d_threshold,
descr,
d_csr_row_ptr,
d_nnz_total_dev_host_ptr,
d_temp_buffer));
std::vector<int> h_nnz_total_copied_from_device(1);
CHECK_HIP_ERROR(hipMemcpy(h_nnz_total_copied_from_device.data(),
d_nnz_total_dev_host_ptr,
sizeof(int),
hipMemcpyDeviceToHost));
if(argus.unit_check)
{
unit_check_general<int>(
1, 1, 1, h_nnz_total_dev_host_ptr.data(), h_nnz_total_copied_from_device.data());
}
auto d_csr_col_ind_managed = hipsparse_unique_ptr{
device_malloc(sizeof(int) * h_nnz_total_dev_host_ptr[0]), device_free};
auto d_csr_val_managed
= hipsparse_unique_ptr{device_malloc(sizeof(T) * h_nnz_total_dev_host_ptr[0]), device_free};
int* d_csr_col_ind = (int*)d_csr_col_ind_managed.get();
T* d_csr_val = (T*)d_csr_val_managed.get();
if(argus.unit_check)
{
CHECK_HIPSPARSE_ERROR(hipsparseSetPointerMode(handle, HIPSPARSE_POINTER_MODE_HOST));
CHECK_HIPSPARSE_ERROR(hipsparseXpruneDense2csr(handle,
M,
N,
d_A,
LDA,
&threshold,
descr,
d_csr_val,
d_csr_row_ptr,
d_csr_col_ind,
d_temp_buffer));
std::vector<int> h_csr_row_ptr(M + 1);
std::vector<int> h_csr_col_ind(h_nnz_total_dev_host_ptr[0]);
std::vector<T> h_csr_val(h_nnz_total_dev_host_ptr[0]);
CHECK_HIP_ERROR(hipMemcpy(
h_csr_row_ptr.data(), d_csr_row_ptr, sizeof(int) * (M + 1), hipMemcpyDeviceToHost));
CHECK_HIP_ERROR(hipMemcpy(h_csr_col_ind.data(),
d_csr_col_ind,
sizeof(int) * h_nnz_total_dev_host_ptr[0],
hipMemcpyDeviceToHost));
CHECK_HIP_ERROR(hipMemcpy(h_csr_val.data(),
d_csr_val,
sizeof(T) * h_nnz_total_dev_host_ptr[0],
hipMemcpyDeviceToHost));
// call host and check results
std::vector<int> h_nnz_cpu(1);
std::vector<int> h_csr_row_ptr_cpu;
std::vector<int> h_csr_col_ind_cpu;
std::vector<T> h_csr_val_cpu;
host_prune_dense2csr(M,
N,
h_A,
LDA,
idx_base,
threshold,
h_nnz_cpu[0],
h_csr_val_cpu,
h_csr_row_ptr_cpu,
h_csr_col_ind_cpu);
unit_check_general<int>(1, 1, 1, h_nnz_cpu.data(), h_nnz_total_dev_host_ptr.data());
unit_check_general<int>(1, (M + 1), 1, h_csr_row_ptr_cpu.data(), h_csr_row_ptr.data());
unit_check_general<int>(
1, h_nnz_total_dev_host_ptr[0], 1, h_csr_col_ind_cpu.data(), h_csr_col_ind.data());
unit_check_general<T>(
1, h_nnz_total_dev_host_ptr[0], 1, h_csr_val_cpu.data(), h_csr_val.data());
}
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(hipsparseXpruneDense2csr(handle,
M,
N,
d_A,
LDA,
&threshold,
descr,
d_csr_val,
d_csr_row_ptr,
d_csr_col_ind,
d_temp_buffer));
}
double gpu_time_used = get_time_us();
// Performance run
for(int iter = 0; iter < number_hot_calls; ++iter)
{
CHECK_HIPSPARSE_ERROR(hipsparseXpruneDense2csr(handle,
M,
N,
d_A,
LDA,
&threshold,
descr,
d_csr_val,
d_csr_row_ptr,
d_csr_col_ind,
d_temp_buffer));
}
gpu_time_used = (get_time_us() - gpu_time_used) / number_hot_calls;
double gbyte_count = prune_dense2csr_gbyte_count<T>(M, N, h_nnz_total_dev_host_ptr[0]);
double gpu_gbyte = get_gpu_gbyte(gpu_time_used, gbyte_count);
display_timing_info(display_key_t::M,
M,
display_key_t::N,
N,
display_key_t::LD,
LDA,
display_key_t::nnz,
h_nnz_total_dev_host_ptr[0],
display_key_t::threshold,
threshold,
display_key_t::bandwidth,
gpu_gbyte,
display_key_t::time_ms,
get_gpu_time_msec(gpu_time_used));
}
#endif
return HIPSPARSE_STATUS_SUCCESS;
}
#endif // TESTING_PRUNE_DENSE2CSR_HPP
|