From c01eb1078ffd6b8736a7a5e58f09b5b1ad7bd076 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Thu, 24 Oct 2013 22:42:04 +0000 Subject: [PATCH] Microbenchmark for small and large allocations of device and host memory. --- microbench/mallocSpeed.cu | 113 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 microbench/mallocSpeed.cu diff --git a/microbench/mallocSpeed.cu b/microbench/mallocSpeed.cu new file mode 100644 index 0000000..270e73b --- /dev/null +++ b/microbench/mallocSpeed.cu @@ -0,0 +1,113 @@ +/* + * + * mallocSpeed.cu + * + * Microbenchmark for overhead and per-page speed of host and + * device memory allocation. + * + * Build with: nvcc -I ../chLib mallocSpeed.cu + * Requires: No minimum SM requirement. + * + * Copyright (c) 2013, Archaea Software, LLC. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "chTimer.h" + +template +float +mallocSpeed( int cIterations, size_t N ) +{ + chTimerTimestamp start, stop; + float ret = 0.0f; + void **p = (void **) malloc( cIterations*sizeof(void *)); + if ( ! p ) + goto Error; + memset( p, 0, cIterations*sizeof(void *) ); + + chTimerGetTime( &start ); + for ( size_t i = 0; i < cIterations; i++ ) { + if ( bDevice ) { + if ( cudaSuccess != cudaMalloc( &p[i], N ) ) { + goto Error; + } + } + else { + if ( cudaSuccess != cudaMallocHost( &p[i], N ) ) + goto Error; + } + } + chTimerGetTime( &stop ); + ret = chTimerElapsedTime( &start, &stop ); +Error: + if ( p ) { + for ( size_t i = 0; i < cIterations; i++ ) { + if ( bDevice ) { + cudaFree( p[i] ); + } + else { + cudaFreeHost( p[i] ); + } + } + free( p ); + } + return ret; +} + +template +float +mallocSpeed( size_t N ) +{ + float ret = 0.0f; + int cIterations; + for ( cIterations = 1; ret < 0.5f; cIterations *= 2 ) { + ret = mallocSpeed( cIterations, N ); + if ( 0.0f == ret ) { + return mallocSpeed( cIterations/2, N) / (cIterations/2); + } + } + return ret / cIterations; +} + + +int +main( int argc, char *argv[] ) +{ + if ( cudaSuccess != cudaFree(0) ) { + printf( "Initialization failed\n" ); + exit(1); + } + printf( "mallocSpeed (4K device): %.2f us/iteration\n", mallocSpeed(4096)*1e6 ); + printf( "mallocSpeed (1M device): %.2f us/iteration\n", mallocSpeed(1048576)*1e6 ); + printf( "mallocSpeed (4K host): %.2f us/iteration\n", mallocSpeed(4096)*1e6 ); + printf( "mallocSpeed (1M host): %.2f us/iteration\n", mallocSpeed(1048576)*1e6 ); + return 0; + +}