     }
 
 #if 0
+    // This does not work
     err = cudaMemcpyToArray( _dog_3d,
                              0, 0,
                              array,
                              10*4*2*sizeof(float),
                              // 10*4*sizeof(float),
                              cudaMemcpyHostToDevice );
-#else
+#endif
     cudaMemcpy3DParms s = { 0 };
     s.srcPtr = make_cudaPitchedPtr( array, 10*sizeof(float), 10, 4 );
     s.dstArray = _dog_3d;
     s.extent = make_cudaExtent( width, height, levels );
     s.kind = cudaMemcpyHostToDevice;
     err = cudaMemcpy3D( &s );
-#endif
+
     if( err != cudaSuccess ) {
         cerr << "CUDA memcpy 3D failed: " << cudaGetErrorString(err) << endl;
+        exit( -1 );
     } else {
         cerr << "CUDA memcpy 3D succeeded" << endl;
     }
+
+    cudaDeviceSynchronize();
+
+    cerr << "== CUDA-sided printing of array ==" << endl;
+
+    testPrintArray
+        <<<1,1>>>
+        ( _dog_3d_tex, 10, 4, 2 );
+        // ( _dog_3d_tex, width, height, levels );
+
+    err = cudaGetLastError( );
+    if( err != cudaSuccess ) {
+        cerr << "testPrintArray: " << cudaGetErrorString(err) << endl;
+        exit( -1 );
+    } else {
+        cerr << "testPrintArray succeeded" << endl;
+    }
+
+#ifdef WITH_SURFACE
+    cerr << "== CUDA-sided writing to surface" << endl;
+
+    dim3 block( 8, 8, 1 );
+    writeToSurface
+        <<<1,block>>>
+        ( _dog_3d_surf, width, height, levels );
+
+    err = cudaGetLastError( );
+    if( err != cudaSuccess ) {
+        cerr << "writeToSurface: " << cudaGetErrorString(err) << endl;
+        exit( -1 );
+    } else {
+        cerr << "writeToSurface succeeded" << endl;
+    }
+
+    cerr << "== CUDA-sided printing of array ==" << endl;
 
     testPrintArray
         <<<1,1>>>
         ( _dog_3d_tex, width, height, levels );
 
+    err = cudaGetLastError( );
+    if( err != cudaSuccess ) {
+        cerr << "testPrintArray: " << cudaGetErrorString(err) << endl;
+        exit( -1 );
+    } else {
+        cerr << "testPrintArray succeeded" << endl;
+    }
+#endif // WITH_SURFACE
+
+    cudaDeviceSynchronize();
+
+    memset( array, 0, width*height*levels*sizeof(float) );
+
+    memset( &s, 0, sizeof(cudaMemcpy3DParms) );
+    s.srcArray = _dog_3d;
+    s.dstPtr = make_cudaPitchedPtr( array, width*sizeof(float), width, height );
+    s.extent = make_cudaExtent( width, height, levels );
+    s.kind = cudaMemcpyDeviceToHost;
+    err = cudaMemcpy3D( &s );
+
+    if( err != cudaSuccess ) {
+        cerr << "CUDA memcpy 3D failed: " << cudaGetErrorString(err) << endl;
+        exit( -1 );
+    } else {
+        cerr << "CUDA memcpy 3D succeeded" << endl;
+    }
+
+    cerr << "== host-sided printing of array ==" << endl;
+    for( int z=0; z<2; z++ ) {
+        for( int y=0; y<4; y++ ) {
+            for( int x=0; x<10; x++ ) {
+                cerr << array[(z*height + y)*width + x] << " ";
+            }
+            cerr << endl;
+        }
+        cerr << endl;
+    }
+
     err = cudaFreeHost( array );
     if( err != cudaSuccess ) {
         cerr << "CUDA free host failed" << endl;
+        exit( -1 );
     } else {
         cerr << "CUDA free host success" << endl;
     }
 
+#ifdef WITH_SURFACE
+    err = cudaDestroySurfaceObject( _dog_3d_surf );
+    if( err != cudaSuccess ) {
+        cerr << "CUDA destroy surface failed" << endl;
+        exit( -1 );
+    } else {
+        cerr << "CUDA destroy surface success" << endl;
+    }
+#endif // WITH_SURFACE
+
     err = cudaDestroyTextureObject( _dog_3d_tex );
     if( err != cudaSuccess ) {
         cerr << "CUDA destroy texture failed" << endl;
+        exit( -1 );
     } else {
         cerr << "CUDA destroy texture success" << endl;
     }

#include <cuda_runtime.h>
#include <iostream>

using namespace std;


    cudaChannelFormatDesc _dog_3d_desc;
    cudaExtent            _dog_3d_ext;
    cudaTextureObject_t   _dog_3d_tex;
#ifdef WITH_SURFACE
    cudaSurfaceObject_t   _dog_3d_surf;
#endif // WITH_SURFACE

    _dog_3d_desc.f = cudaChannelFormatKindFloat;
    _dog_3d_desc.x = 32;
