dpstream.cu 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /* Routines to allow CUDA threads to write distinguished points into
  24. a buffer that will be read by the host */
  25. __constant__ static unsigned int *DPbuffer = NULL;
  26. __constant__ static unsigned int DPbuffersize = 0; // in words
  27. static unsigned int *DPbuffer_device = NULL;
  28. __device__ volatile static unsigned int DPbuffertail = 0; // in words
  29. static const unsigned int DPrecordsize = (1+WORDS+6); // in words
  30. static unsigned int DPbufsize = 0;
  31. cudaError_t DPstreamInit(size_t numrecords)
  32. {
  33. // Allocate the buffer
  34. DPbufsize = DPrecordsize * numrecords;
  35. if (cudaMalloc((void **)&DPbuffer_device, DPbufsize * sizeof(unsigned int))
  36. != cudaSuccess) {
  37. return cudaErrorInitializationError;
  38. }
  39. // Zero it
  40. cudaMemset(DPbuffer_device, 0, DPbufsize * sizeof(unsigned int));
  41. // Initialize the device-visible pointers
  42. unsigned int zero = 0;
  43. cudaMemcpyToSymbol(DPbuffer, &DPbuffer_device, sizeof(unsigned int*));
  44. cudaMemcpyToSymbol(DPbuffersize, &DPbufsize, sizeof(unsigned int));
  45. cudaMemcpyToSymbol(DPbuffertail, &zero, sizeof(unsigned int));
  46. return cudaSuccess;
  47. }
  48. void DPstreamEnd(void)
  49. {
  50. if (DPbuffer_device) {
  51. cudaFree(DPbuffer_device);
  52. }
  53. DPbuffer_device = NULL;
  54. }
  55. // Call this from the device to write the following into the buffer:
  56. // block and thread indices (1 word total)
  57. // x (WORDS words)
  58. // a (2 words)
  59. // b (2 words)
  60. __device__ inline unsigned int *DPstreamAlloc()
  61. {
  62. if (!DPbuffer) return NULL;
  63. // Atomically increment the DPbuffertail
  64. if (DPbuffertail >= DPbuffersize) return NULL;
  65. unsigned int ouroffset = atomicAdd((unsigned int *)&DPbuffertail,
  66. (unsigned int) (DPrecordsize));
  67. if (ouroffset + DPrecordsize > DPbuffersize) {
  68. atomicSub((unsigned int *)&DPbuffertail, (unsigned int)DPrecordsize);
  69. return NULL;
  70. }
  71. unsigned int *ourbuffer = DPbuffer + ouroffset;
  72. return ourbuffer;
  73. }
  74. // dp points to an array of WORDS+7 unsigned ints:
  75. // - 1 word of threadID/blockID
  76. // - WORDS words of the dp value
  77. // - 3 words of a
  78. // - 3 words of b
  79. extern bool dpcallback(void *data, unsigned int *dpwords);
  80. // Return true if we should stop computing
  81. bool DPstreamParse(void *data)
  82. {
  83. // Get the number of words in the buffer
  84. unsigned int bufsize;
  85. cudaMemcpyFromSymbol(&bufsize, DPbuffertail, sizeof(unsigned int));
  86. #ifdef VERBOSE
  87. cerr << getpid() << " " << bufsize / DPrecordsize << " DPs\n";
  88. #endif
  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. if (dpcallback(data, dp)) {
  96. stop_computing = true;
  97. }
  98. }
  99. }
  100. free(dpbuf);
  101. // Reset the buffer
  102. unsigned int zero = 0;
  103. cudaMemcpyToSymbol(DPbuffertail, &zero, sizeof(unsigned int));
  104. return stop_computing;
  105. }
  106. #endif