streamcluster.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /***********************************************
  2. streamcluster.h
  3. : header file to streamcluster
  4. - original code from PARSEC Benchmark Suite
  5. - parallelization with CUDA API has been applied by
  6. Sang-Ha (a.k.a Shawn) Lee - sl4ge@virginia.edu
  7. University of Virginia
  8. Department of Electrical and Computer Engineering
  9. Department of Computer Science
  10. :revised by
  11. Jianbin Fang - j.fang@tudelft.nl
  12. Delft University of Technology
  13. Faculty of Electrical Engineering, Mathematics and Computer Science
  14. Department of Software Technology
  15. Parallel and Distributed Systems Group
  16. on 15/03/2011
  17. ***********************************************/
  18. #ifndef STREAMCLUSTER_H
  19. #define STREAMCLUSTER_H
  20. #include <stdio.h>
  21. #include <iostream>
  22. #include <fstream>
  23. #include <stdlib.h>
  24. #include <sys/time.h>
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <math.h>
  28. #include <sys/resource.h>
  29. #include <limits.h>
  30. #ifdef ENABLE_PARSEC_HOOKS
  31. #include <hooks.h>
  32. #endif
  33. using namespace std;
  34. /* this structure represents a point */
  35. /* these will be passed around to avoid copying coordinates */
  36. typedef struct {
  37. float weight;
  38. float * coord;
  39. long long assign; /* number of point where this one is assigned */
  40. float cost; /* cost of that assignment, weight*distance */
  41. } Point;
  42. /* this is the array of points */
  43. typedef struct {
  44. long num; /* number of points; may not be N if this is a sample */
  45. int dim; /* dimensionality */
  46. Point *p; /* the array itself */
  47. } Points;
  48. struct pkmedian_arg_t
  49. {
  50. Points* points;
  51. long kmin;
  52. long kmax;
  53. long* kfinal;
  54. int pid;
  55. pthread_barrier_t* barrier;
  56. };
  57. class PStream {
  58. public:
  59. virtual size_t read( float* dest, int dim, int num ) = 0;
  60. virtual int ferror() = 0;
  61. virtual int feof() = 0;
  62. virtual ~PStream() {
  63. }
  64. };
  65. //synthetic stream
  66. class SimStream : public PStream {
  67. public:
  68. SimStream(long n_ ) {
  69. n = n_;
  70. }
  71. size_t read( float* dest, int dim, int num ) {
  72. size_t count = 0;
  73. for( int i = 0; i < num && n > 0; i++ ) {
  74. for( int k = 0; k < dim; k++ ) {
  75. dest[i*dim + k] = lrand48()/(float)INT_MAX;
  76. }
  77. n--;
  78. count++;
  79. }
  80. return count;
  81. }
  82. int ferror() {
  83. return 0;
  84. }
  85. int feof() {
  86. return n <= 0;
  87. }
  88. ~SimStream() {
  89. }
  90. private:
  91. long n;
  92. };
  93. class FileStream : public PStream {
  94. public:
  95. FileStream(char* filename) {
  96. fp = fopen( filename, "rb");
  97. if( fp == NULL ) {
  98. fprintf(stderr,"error opening file %s\n.",filename);
  99. exit(1);
  100. }
  101. }
  102. size_t read( float* dest, int dim, int num ) {
  103. return std::fread(dest, sizeof(float)*dim, num, fp);
  104. }
  105. int ferror() {
  106. return std::ferror(fp);
  107. }
  108. int feof() {
  109. return std::feof(fp);
  110. }
  111. ~FileStream() {
  112. printf("closing file stream\n");
  113. fclose(fp);
  114. }
  115. private:
  116. FILE* fp;
  117. };
  118. /* function prototypes */
  119. double gettime();
  120. int isIdentical(float*, float*, int);
  121. //static int floatcomp(const void*, const void*);
  122. void shuffle(Points*);
  123. void intshuffle(int*, int);
  124. float waste(float);
  125. float dist(Point, Point, int);
  126. float pspeedy(Points*, float, long, int, pthread_barrier_t*);
  127. float pgain_old(long, Points*, float, long int*, int, pthread_barrier_t*);
  128. float pFL(Points*, int*, int, float, long*, float, long, float, int, pthread_barrier_t*);
  129. int selectfeasible_fast(Points*, int**, int, int, pthread_barrier_t*);
  130. float pkmedian(Points*, long, long, long*, int, pthread_barrier_t*);
  131. int contcenters(Points*);
  132. void copycenters(Points*, Points*, long*, long);
  133. void* localSearchSub(void*);
  134. void localSearch(Points*, long, long, long*);
  135. void outcenterIDs(Points*, long*, char*);
  136. void streamCluster(PStream*, long, long, int, long, long, char*);
  137. float pgain(long, Points*, float, long int*, int, bool*, int*, bool*, double*, double*, double*, double*, double*);
  138. void allocDevMem(int, int, int);
  139. void freeDevMem();
  140. void quit(char*);
  141. #endif