dpstream.cu 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * cudadl version 0.9: Compute discrete logs in smooth group orders
  3. * using CUDA
  4. * Copyright (C) 2012 by Ryan Henry and Ian Goldberg
  5. * {rhenry,iang}@cs.uwaterloo.ca
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of version 3 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __DPSTREAM_H__
  20. #define __DPSTREAM_H__
  21. #include <sm_20_atomic_functions.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. /* Routines to allow CUDA threads to write distinguished points into
  25. a buffer that will be read by the host */
  26. __constant__ static unsigned int *DPbuffer = NULL;
  27. __constant__ static unsigned int DPbuffersize = 0; // in words
  28. static unsigned int *DPbuffer_device = NULL;
  29. __device__ volatile static unsigned int DPbuffertail = 0; // in words
  30. static const unsigned int DPrecordsize = (1+WORDS+6); // in words
  31. static unsigned int DPbufsize = 0;
  32. cudaError_t DPstreamInit(size_t numrecords)
  33. {
  34. // Allocate the buffer
  35. DPbufsize = DPrecordsize * numrecords;
  36. if (cudaMalloc((void **)&DPbuffer_device, DPbufsize * sizeof(unsigned int))
  37. != cudaSuccess) {
  38. return cudaErrorInitializationError;
  39. }
  40. // Zero it
  41. cudaMemset(DPbuffer_device, 0, DPbufsize * sizeof(unsigned int));
  42. // Initialize the device-visible pointers
  43. unsigned int zero = 0;
  44. cudaMemcpyToSymbol(DPbuffer, &DPbuffer_device, sizeof(unsigned int*));
  45. cudaMemcpyToSymbol(DPbuffersize, &DPbufsize, sizeof(unsigned int));
  46. cudaMemcpyToSymbol(DPbuffertail, &zero, sizeof(unsigned int));
  47. return cudaSuccess;
  48. }
  49. void DPstreamEnd(void)
  50. {
  51. if (DPbuffer_device) {
  52. cudaFree(DPbuffer_device);
  53. }
  54. DPbuffer_device = NULL;
  55. }
  56. // Call this from the device to write the following into the buffer:
  57. // block and thread indices (1 word total)
  58. // x (WORDS words)
  59. // a (2 words)
  60. // b (2 words)
  61. __device__ inline unsigned int *DPstreamAlloc()
  62. {
  63. if (!DPbuffer) return NULL;
  64. // Atomically increment the DPbuffertail
  65. if (DPbuffertail >= DPbuffersize) return NULL;
  66. unsigned int ouroffset = atomicAdd((unsigned int *)&DPbuffertail,
  67. (unsigned int) (DPrecordsize));
  68. if (ouroffset + DPrecordsize > DPbuffersize) {
  69. atomicSub((unsigned int *)&DPbuffertail, (unsigned int)DPrecordsize);
  70. return NULL;
  71. }
  72. unsigned int *ourbuffer = DPbuffer + ouroffset;
  73. return ourbuffer;
  74. }
  75. #ifdef DERANDOMIZE
  76. int DPrecordComparator(const void* p1, const void* p2)
  77. {
  78. const unsigned int* p1_uint = (const unsigned int*)p1;
  79. const unsigned int* p2_uint = (const unsigned int*)p2;
  80. for (int i=0; i<DPrecordsize; i++) {
  81. // compare each word
  82. if (p1_uint[i] < p2_uint[i]) {
  83. return 1;
  84. } else if (p1_uint[i] > p2_uint[i]) {
  85. return -1;
  86. }
  87. }
  88. return 0;
  89. }
  90. #endif
  91. // dp points to an array of WORDS+7 unsigned ints:
  92. // - 1 word of threadID/blockID
  93. // - WORDS words of the dp value
  94. // - 3 words of a
  95. // - 3 words of b
  96. extern bool dpcallback(void *data, unsigned int *dpwords);
  97. // Return true if we should stop computing
  98. bool DPstreamParse(void *data, unsigned int *num_dps, bool *filled_dp_buffer)
  99. {
  100. // Get the number of words in the buffer
  101. unsigned int bufsize;
  102. cudaMemcpyFromSymbol(&bufsize, DPbuffertail, sizeof(unsigned int));
  103. unsigned int max_bufsize;
  104. cudaMemcpyFromSymbol(&max_bufsize, DPbuffersize, sizeof(unsigned int));
  105. *num_dps = bufsize/DPrecordsize;
  106. *filled_dp_buffer = (bufsize == max_bufsize);
  107. unsigned int *dpbuf = (unsigned int*)calloc(bufsize, sizeof(unsigned int));
  108. bool stop_computing = false;
  109. if (dpbuf) {
  110. cudaMemcpy(dpbuf, DPbuffer_device, bufsize * sizeof(unsigned int),
  111. cudaMemcpyDeviceToHost);
  112. #ifdef DERANDOMIZE
  113. qsort(dpbuf, *num_dps, DPrecordsize*sizeof(unsigned int), DPrecordComparator);
  114. // make the order of the DPs deterministic after each kernel launch
  115. #endif
  116. for (unsigned int *dp=dpbuf; dp < dpbuf+bufsize; dp += DPrecordsize) {
  117. if (dpcallback(data, dp)) {
  118. stop_computing = true;
  119. }
  120. }
  121. }
  122. free(dpbuf);
  123. // Reset the buffer
  124. unsigned int zero = 0;
  125. cudaMemcpyToSymbol(DPbuffertail, &zero, sizeof(unsigned int));
  126. return stop_computing;
  127. }
  128. #endif