dpstream.cu 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_11_atomic_functions.h>
  22. /* Routines to allow CUDA threads to write distinguished points into
  23. a buffer that will be read by the host */
  24. __constant__ static unsigned int *DPbuffer = NULL;
  25. __constant__ static unsigned int DPbuffersize = 0; // in words
  26. static unsigned int *DPbuffer_device = NULL;
  27. __device__ volatile static unsigned int DPbuffertail = 0; // in words
  28. static const unsigned int DPrecordsize = (1+WORDS+6); // in words
  29. static unsigned int DPbufsize = 0;
  30. cudaError_t DPstreamInit(size_t numrecords)
  31. {
  32. // Allocate the buffer
  33. DPbufsize = DPrecordsize * numrecords;
  34. if (cudaMalloc((void **)&DPbuffer_device, DPbufsize * sizeof(unsigned int))
  35. != cudaSuccess) {
  36. return cudaErrorInitializationError;
  37. }
  38. // Zero it
  39. cudaMemset(DPbuffer_device, 0, DPbufsize * sizeof(unsigned int));
  40. // Initialize the device-visible pointers
  41. unsigned int zero = 0;
  42. cudaMemcpyToSymbol(DPbuffer, &DPbuffer_device, sizeof(unsigned int*));
  43. cudaMemcpyToSymbol(DPbuffersize, &DPbufsize, sizeof(unsigned int));
  44. cudaMemcpyToSymbol(DPbuffertail, &zero, sizeof(unsigned int));
  45. return cudaSuccess;
  46. }
  47. void DPstreamEnd(void)
  48. {
  49. if (DPbuffer_device) {
  50. cudaFree(DPbuffer_device);
  51. }
  52. DPbuffer_device = NULL;
  53. }
  54. // Call this from the device to write the following into the buffer:
  55. // block and thread indices (1 word total)
  56. // x (WORDS words)
  57. // a (2 words)
  58. // b (2 words)
  59. __device__ inline unsigned int *DPstreamAlloc()
  60. {
  61. if (!DPbuffer) return NULL;
  62. // Atomically increment the DPbuffertail
  63. if (DPbuffertail >= DPbuffersize) return NULL;
  64. unsigned int ouroffset = atomicAdd((unsigned int *)&DPbuffertail,
  65. (unsigned int) (DPrecordsize));
  66. if (ouroffset + DPrecordsize > DPbuffersize) {
  67. atomicSub((unsigned int *)&DPbuffertail, (unsigned int)DPrecordsize);
  68. return NULL;
  69. }
  70. unsigned int *ourbuffer = DPbuffer + ouroffset;
  71. return ourbuffer;
  72. }
  73. extern bool dpcallback(void *data, unsigned short threadId,
  74. unsigned short blockId, unsigned int demux, string x,
  75. unsigned int a_0, unsigned int a_1, unsigned int a_2,
  76. unsigned int b_0, unsigned int b_1, unsigned int b_2);
  77. #if WORDS > 1
  78. #define DEMUXWORD 1
  79. #else
  80. #define DEMUXWORD 0
  81. #endif
  82. // Return true if we should stop computing
  83. bool DPstreamParse(void *data)
  84. {
  85. // Get the number of words in the buffer
  86. unsigned int bufsize;
  87. cudaMemcpyFromSymbol(&bufsize, DPbuffertail, sizeof(unsigned int));
  88. cerr << getpid() << " " << bufsize / DPrecordsize << " DPs\n";
  89. unsigned int *dpbuf = (unsigned int*)calloc(bufsize, sizeof(unsigned int));
  90. bool stop_computing = false;
  91. if (dpbuf) {
  92. cudaMemcpy(dpbuf, DPbuffer_device, bufsize * sizeof(unsigned int),
  93. cudaMemcpyDeviceToHost);
  94. for (unsigned int *dp=dpbuf; dp < dpbuf+bufsize; dp += DPrecordsize) {
  95. unsigned short threadId = (unsigned short)(dp[0]);
  96. unsigned short blockId = dp[0]>>16;
  97. unsigned int a_0 = dp[WORDS+1];
  98. unsigned int a_1 = dp[WORDS+2];
  99. unsigned int a_2 = dp[WORDS+3];
  100. unsigned int b_0 = dp[WORDS+4];
  101. unsigned int b_1 = dp[WORDS+5];
  102. unsigned int b_2 = dp[WORDS+6];
  103. string x((const char *)(dp+1), WORDS*sizeof(unsigned int));
  104. if (dpcallback(data, threadId, blockId, dp[DEMUXWORD], x, a_0, a_1, a_2, b_0, b_1, b_2)) {
  105. stop_computing = true;
  106. }
  107. }
  108. }
  109. free(dpbuf);
  110. // Reset the buffer
  111. unsigned int zero = 0;
  112. cudaMemcpyToSymbol(DPbuffertail, &zero, sizeof(unsigned int));
  113. return stop_computing;
  114. }
  115. #endif