CUDA Networks
src
linear_algebra
vector_print.cu
Go to the documentation of this file.
1
/**
2
* @file vector_print.cu
3
* @brief Implementation of the Vector::print method with consistent spacing.
4
*/
5
#include "
vector.h
"
6
#include <cuda_runtime.h>
7
#include <iostream>
8
#include <iomanip>
9
#include <cstdio>
10
11
void
Vector::print
(
int
decimals) {
12
// Create format string for desired number of decimals
13
char
format[20];
14
sprintf(format,
"%%d:\t%%.%df\n"
, decimals);
15
16
// Allocate host memory to copy the data from GPU
17
double
* h_data =
new
double
[rows];
18
cudaMemcpy(h_data, d_data, rows *
sizeof
(
double
), cudaMemcpyDeviceToHost);
19
20
// Print vector dimensions
21
std::cout <<
"Vector with "
<< rows <<
" rows:\n"
;
22
23
// Print column header (since vector is treated as a single column)
24
std::cout <<
"\t0:\t\n"
;
25
26
// Iterate over rows
27
for
(
int
i = 0; i < rows; ++i) {
28
// If more than 10 rows, only print first and last 5
29
if
(i == 5 && rows > 10) {
30
std::cout <<
"...\t...\n"
;
31
i = rows - 5;
// Skip to the last 5 rows
32
}
33
// Print row index and value
34
printf(format, i, h_data[i]);
35
}
36
37
// Free the allocated host memory
38
delete
[] h_data;
39
std::cout << std::endl;
40
}
Vector::print
void print(int decimals)
Print the vector contents.
Definition:
vector_print.cu:11
vector.h
Defines the Vector class for GPU-accelerated vector operations.
Generated by
1.9.1