MAGMA  1.2.0
MatrixAlgebraonGPUandMulticoreArchitectures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
cprint.cpp
Go to the documentation of this file.
1 /*
2  -- MAGMA (version 1.2.0) --
3  Univ. of Tennessee, Knoxville
4  Univ. of California, Berkeley
5  Univ. of Colorado, Denver
6  May 2012
7 
8  @author Mark Gates
9  @generated c Thu May 10 22:26:26 2012
10 
11 */
12 #include "common_magma.h"
13 
14 // -------------------------
15 // Returns:
16 // 1 if A is a device pointer (definitely),
17 // 0 if A is a host pointer (definitely or inferred from error),
18 // -1 if unknown.
19 // On 2.0 cards with unified addressing, CUDA can tell if this is a device pointer.
20 // For malloc'd host pointers, cudaPointerGetAttributes returns error.
21 static int is_devptr( void* A )
22 {
23  cudaError_t err;
24  cudaDeviceProp prop;
25  cudaPointerAttributes attr;
26  int dev;
27  err = cudaGetDevice( &dev );
28  if ( ! err ) {
29  err = cudaGetDeviceProperties( &prop, dev );
30  if ( ! err and prop.unifiedAddressing ) {
31  err = cudaPointerGetAttributes( &attr, A );
32  if ( ! err ) {
33  // definitely know type
34  return (attr.memoryType == cudaMemoryTypeDevice);
35  }
36  else if ( err == cudaErrorInvalidValue ) {
37  // infer as host pointer
38  return 0;
39  }
40  }
41  }
42  // unknown, e.g., device doesn't support unified addressing
43  return -1;
44 }
45 
46 
47 #define A(i,j) (A + i + j*lda)
48 
49 // -------------------------
50 // Prints a matrix that is on the CPU host.
51 extern "C"
52 void magma_cprint( int m, int n, cuFloatComplex *A, int lda )
53 {
54  if ( is_devptr( A ) == 1 ) {
55  fprintf( stderr, "ERROR: cprint called with device pointer.\n" );
56  exit(1);
57  }
58 
59  cuFloatComplex c_zero = MAGMA_C_ZERO;
60 
61  printf( "[\n" );
62  for( int i = 0; i < m; ++i ) {
63  for( int j = 0; j < n; ++j ) {
64  if ( MAGMA_C_EQUAL( *A(i,j), c_zero )) {
65  printf( " 0. " );
66  }
67  else {
68  printf( " %8.4f", MAGMA_C_REAL( *A(i,j) ));
69  }
70  }
71  printf( "\n" );
72  }
73  printf( "];\n" );
74 }
75 
76 // -------------------------
77 // Prints a matrix that is on the GPU device.
78 // Internally allocates memory on host, copies it to the host, prints it,
79 // and de-allocates host memory.
80 extern "C"
81 void magma_cprint_gpu( int m, int n, cuFloatComplex *dA, int ldda )
82 {
83  if ( is_devptr( dA ) == 0 ) {
84  fprintf( stderr, "ERROR: cprint_gpu called with host pointer.\n" );
85  exit(1);
86  }
87 
88  int lda = m;
89  cuFloatComplex* A = (cuFloatComplex*) malloc( lda*n*sizeof(cuFloatComplex) );
90  cublasGetMatrix( m, n, sizeof(cuFloatComplex), dA, ldda, A, lda );
91 
92  magma_cprint( m, n, A, lda );
93 
94  free( A );
95 }