alloc_test.cu 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * nvbio
  3. * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the NVIDIA CORPORATION nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // alloc_test.cu
  28. //
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <vector>
  32. #include <algorithm>
  33. #include <nvbio/basic/timer.h>
  34. #include <nvbio/basic/console.h>
  35. #include <nvbio/basic/cuda/arch.h>
  36. namespace nvbio {
  37. int alloc_test()
  38. {
  39. log_info( stderr, "alloc test... started\n" );
  40. const uint32 N_TESTS = 32;
  41. for (size_t size = 1024*1024; size <= size_t(1u << 30); size *= 4)
  42. {
  43. Timer timer;
  44. float cuda_malloc_time = 0.0f;
  45. float cuda_free_time = 0.0f;
  46. float malloc_time = 0.0f;
  47. float free_time = 0.0f;
  48. for (uint32 i = 0; i < N_TESTS; ++i)
  49. {
  50. void* ptr;
  51. // cuda
  52. timer.start();
  53. cudaMalloc( &ptr, size );
  54. timer.stop();
  55. cuda_malloc_time += timer.seconds();
  56. timer.start();
  57. cudaFree( ptr );
  58. timer.stop();
  59. cuda_free_time += timer.seconds();
  60. // cpu
  61. timer.start();
  62. ptr = malloc( size );
  63. timer.stop();
  64. malloc_time += timer.seconds();
  65. timer.start();
  66. free( ptr );
  67. timer.stop();
  68. free_time += timer.seconds();
  69. }
  70. const float GB = float(1024*1024*1024);
  71. cuda_malloc_time /= N_TESTS;
  72. cuda_free_time /= N_TESTS;
  73. malloc_time /= N_TESTS;
  74. free_time /= N_TESTS;
  75. log_info( stderr, " %u MB:\n", size/(1024*1024) );
  76. log_info( stderr, " cuda malloc : %.2f ms, %.3f GB/s\n", cuda_malloc_time*1000.0f, (float(size)/(cuda_malloc_time)) / GB );
  77. log_info( stderr, " cuda free : %.2f ms, %.3f GB/s\n", cuda_free_time*1000.0f, (float(size)/(cuda_free_time)) / GB );
  78. log_info( stderr, " malloc : %.2f ms, %.3f GB/s\n", malloc_time*1000.0f, (float(size)/(malloc_time)) / GB );
  79. log_info( stderr, " free : %.2f ms, %.3f GB/s\n", free_time*1000.0f, (float(size)/(free_time)) / GB );
  80. }
  81. log_info( stderr, "alloc test... done\n" );
  82. return 0;
  83. }
  84. } // namespace nvbio