datagen.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * datagen.cpp
  3. * by Sam Kauffman - University of Virginia
  4. *
  5. * This program generates datasets for Rodinia's kmeans
  6. *
  7. * Usage:
  8. * datagen <numObjects> [ <numFeatures> ] [-f]
  9. *
  10. * numFeatures defaults to 34
  11. * Features are integers from 0 to 255 by default
  12. * With -f, features are floats from 0.0 to 1.0
  13. * Output file is "<numObjects>_<numFeatures>.txt"
  14. * One object per line. The first number in each line is an object ID and is
  15. * ignored by kmeans.
  16. *
  17. */
  18. #include <iostream>
  19. #include <fstream>
  20. #include <sstream>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include <ctime>
  24. using namespace std;
  25. int main( int argc, char * argv[] )
  26. {
  27. if ( argc < 2 )
  28. {
  29. cerr << "Error: Specify a number of objects to generate.\n";
  30. exit( 1 );
  31. }
  32. int nObj = atoi( argv[1] );
  33. int nFeat = 0;
  34. if ( argc > 2 )
  35. nFeat = atoi( argv[2] );
  36. if ( nFeat == 0 && argc > 3 )
  37. nFeat = atoi( argv[3] );
  38. if ( nFeat == 0 )
  39. nFeat = 34;
  40. if ( nObj < 1 || nFeat < 1 )
  41. {
  42. cerr << "Error: Invalid argument(s).\n";
  43. exit( 1 );
  44. }
  45. bool floats = false;
  46. if ( ( argc > 2 && strcmp( argv[2], "-f" ) == 0 ) || ( argc > 3 && strcmp( argv[2], "-f" ) == 0 ) )
  47. floats = true;
  48. stringstream ss;
  49. string f = floats ? "f" : "";
  50. ss << nObj << "_" << nFeat << f << ".txt";
  51. ofstream outf( ss.str().c_str(), ios::out | ios::trunc );
  52. srand( time( NULL ) );
  53. if ( !floats )
  54. for ( int i = 0; i < nObj; i++ )
  55. {
  56. outf << i << " ";
  57. for ( int j = 0; j < nFeat; j++ )
  58. outf << ( rand() % 256 ) << " ";
  59. outf << endl;
  60. }
  61. else
  62. {
  63. outf.precision( 4 );
  64. outf.setf( ios::fixed );
  65. for ( int i = 0; i < nObj; i++ )
  66. {
  67. outf << i << " ";
  68. for ( int j = 0; j < nFeat; j++ )
  69. outf << ( (float)rand() / (float)RAND_MAX ) << " ";
  70. outf << endl;
  71. }
  72. }
  73. outf.close();
  74. string type = floats ? " \"float\"" : " \"int\"";
  75. cout << "Generated " << nObj << " objects with " << nFeat << type << " features in " << ss.str() << ".\n";
  76. }