Changeset 2385 for trunk/Clp/src/ClpMain.cpp
 Timestamp:
 Jan 6, 2019 2:43:06 PM (4 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Clp/src/ClpMain.cpp
r2084 r2385 12 12 #endif 13 13 #ifndef ABC_INHERIT 14 void ClpMain0(ClpSimplex * 15 int ClpMain1(int argc, const char *argv[], ClpSimplex *model);14 void ClpMain0(ClpSimplex *models); 15 int ClpMain1(int argc, const char *argv[], ClpSimplex *model); 16 16 #else 17 void ClpMain0(AbcSimplex * 18 int ClpMain1(int argc, const char *argv[], AbcSimplex *model);19 #endif 17 void ClpMain0(AbcSimplex *models); 18 int ClpMain1(int argc, const char *argv[], AbcSimplex *model); 19 #endif 20 20 //#define CILK_TEST 21 21 #ifdef CILK_TEST … … 29 29 */ 30 30 #if CLP_USE_OPENBLAS 31 extern "C" 32 { 33 void openblas_set_num_threads(int num_threads); 31 extern "C" { 32 void openblas_set_num_threads(int num_threads); 34 33 } 35 34 #endif … … 39 38 #define COIN_FACTORIZATION_DENSE_CODE 1 40 39 #endif 41 #if COIN_FACTORIZATION_DENSE_CODE ==140 #if COIN_FACTORIZATION_DENSE_CODE == 1 42 41 // using simple lapack interface 43 extern "C" 44 { 45 void openblas_set_num_threads(int num_threads); 42 extern "C" { 43 void openblas_set_num_threads(int num_threads); 46 44 #if 0 47 45 /** LAPACK Fortran subroutine DGETRF. */ … … 58 56 int test_lapack(int n) 59 57 { 60 int *ipiv;58 int *ipiv; 61 59 int info; 62 60 int i, j; 63 double * 64 65 int LDB, LDA, N, NRHS;61 double *m, *x, *y; 62 63 int LDB, LDA, N, NRHS; 66 64 char transp = 'N'; 67 68 69 m=(double*)malloc(sizeof(double)*n*n); 70 x=(double*)malloc(sizeof(double)*n); 71 y=(double*)malloc(sizeof(double)*n); 72 ipiv=(int*)malloc(sizeof(int)*n); 73 74 for (i=0; i<n; ++i) { 75 x[i]=1.0; 76 for (j=0; j<n; ++j) { 77 m[i*n+j]=(rand()%100+1)/10.0; 78 // printf("m[%d,%d]=%lf\n",i,j, m[i*n+j]); 65 66 m = (double *)malloc(sizeof(double) * n * n); 67 x = (double *)malloc(sizeof(double) * n); 68 y = (double *)malloc(sizeof(double) * n); 69 ipiv = (int *)malloc(sizeof(int) * n); 70 71 for (i = 0; i < n; ++i) { 72 x[i] = 1.0; 73 for (j = 0; j < n; ++j) { 74 m[i * n + j] = (rand() % 100 + 1) / 10.0; 75 // printf("m[%d,%d]=%lf\n",i,j, m[i*n+j]); 79 76 } 80 77 } 81 78 82 79 /* test cblas.h */ 83 80 //cblas_dgemv(CblasColMajor, CblasNoTrans, n, n, 1.0, m, n, 84 81 // x, 1, 0.0, y, 1); 85 86 // for (i=0; i<n; ++i) printf("x[%d]=%lf\n",i, x[i]); 87 //for (i=0; i<n; ++i) printf("y[%d]=%lf\n",i, y[i]); 88 89 LDB=n; 90 LDA=n; 91 N=n; 92 NRHS=1; 93 info=0; 94 95 LAPACK_dgetrf(&N, &N, m, &LDA,ipiv, &info); 96 97 if (info != 0) fprintf(stderr, "dgetrf failure with error %d\n", info); 98 82 83 // for (i=0; i<n; ++i) printf("x[%d]=%lf\n",i, x[i]); 84 //for (i=0; i<n; ++i) printf("y[%d]=%lf\n",i, y[i]); 85 86 LDB = n; 87 LDA = n; 88 N = n; 89 NRHS = 1; 90 info = 0; 91 92 LAPACK_dgetrf(&N, &N, m, &LDA, ipiv, &info); 93 94 if (info != 0) 95 fprintf(stderr, "dgetrf failure with error %d\n", info); 96 99 97 LAPACK_dgetrs(&transp, &N, &NRHS, m, &LDA, ipiv, y, &LDB, &info); 100 101 if (info != 0) fprintf(stderr, "failure with error %d\n", info); 98 99 if (info != 0) 100 fprintf(stderr, "failure with error %d\n", info); 102 101 // for (i=0; i<n; ++i) printf("%lf\n", y[i]); 103 102 … … 108 107 return 0; 109 108 } 110 #elif COIN_FACTORIZATION_DENSE_CODE ==2109 #elif COIN_FACTORIZATION_DENSE_CODE == 2 111 110 // C interface 112 enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102}; 113 enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112}; 114 extern "C" 115 { 116 int clapack_dgetrf ( const enum CBLAS_ORDER Order, const int M, const int N, double *A, const int lda, int *ipiv ); 117 int clapack_dgetrs ( const enum CBLAS_ORDER Order, 118 const enum CBLAS_TRANSPOSE Trans, 119 const int N, const int NRHS, 120 const double *A, const int lda, const int *ipiv, double *B, 121 const int ldb ); 111 enum CBLAS_ORDER { CblasRowMajor = 101, 112 CblasColMajor = 102 }; 113 enum CBLAS_TRANSPOSE { CblasNoTrans = 111, 114 CblasTrans = 112 }; 115 extern "C" { 116 int clapack_dgetrf(const enum CBLAS_ORDER Order, const int M, const int N, double *A, const int lda, int *ipiv); 117 int clapack_dgetrs(const enum CBLAS_ORDER Order, 118 const enum CBLAS_TRANSPOSE Trans, 119 const int N, const int NRHS, 120 const double *A, const int lda, const int *ipiv, double *B, 121 const int ldb); 122 122 } 123 123 int test_lapack(int n) 124 124 { 125 int *ipiv;125 int *ipiv; 126 126 int info; 127 127 int i, j; 128 double * 129 130 int LDB, LDA, N, NRHS;128 double *m, *x, *y; 129 130 int LDB, LDA, N, NRHS; 131 131 char transp = 'N'; 132 133 134 m=(double*)malloc(sizeof(double)*n*n); 135 x=(double*)malloc(sizeof(double)*n); 136 y=(double*)malloc(sizeof(double)*n); 137 ipiv=(int*)malloc(sizeof(int)*n); 138 139 for (i=0; i<n; ++i) { 140 x[i]=1.0; 141 for (j=0; j<n; ++j) { 142 m[i*n+j]=(rand()%100+1)/10.0; 143 // printf("m[%d,%d]=%lf\n",i,j, m[i*n+j]); 132 133 m = (double *)malloc(sizeof(double) * n * n); 134 x = (double *)malloc(sizeof(double) * n); 135 y = (double *)malloc(sizeof(double) * n); 136 ipiv = (int *)malloc(sizeof(int) * n); 137 138 for (i = 0; i < n; ++i) { 139 x[i] = 1.0; 140 for (j = 0; j < n; ++j) { 141 m[i * n + j] = (rand() % 100 + 1) / 10.0; 142 // printf("m[%d,%d]=%lf\n",i,j, m[i*n+j]); 144 143 } 145 144 } 146 145 147 146 /* test cblas.h */ 148 147 //cblas_dgemv(CblasColMajor, CblasNoTrans, n, n, 1.0, m, n, 149 148 // x, 1, 0.0, y, 1); 150 151 // for (i=0; i<n; ++i) printf("x[%d]=%lf\n",i, x[i]); 152 //for (i=0; i<n; ++i) printf("y[%d]=%lf\n",i, y[i]); 153 154 LDB=n; 155 LDA=n; 156 N=n; 157 NRHS=1; 158 info=clapack_dgetrf ( CblasColMajor,n,n,m,n,ipiv ); 159 160 if (info != 0) fprintf(stderr, "dgetrf failure with error %d\n", info); 161 162 clapack_dgetrs ( CblasColMajor,CblasNoTrans,n,1,m,n,ipiv,y,n); 163 164 if (info != 0) fprintf(stderr, "failure with error %d\n", info); 149 150 // for (i=0; i<n; ++i) printf("x[%d]=%lf\n",i, x[i]); 151 //for (i=0; i<n; ++i) printf("y[%d]=%lf\n",i, y[i]); 152 153 LDB = n; 154 LDA = n; 155 N = n; 156 NRHS = 1; 157 info = clapack_dgetrf(CblasColMajor, n, n, m, n, ipiv); 158 159 if (info != 0) 160 fprintf(stderr, "dgetrf failure with error %d\n", info); 161 162 clapack_dgetrs(CblasColMajor, CblasNoTrans, n, 1, m, n, ipiv, y, n); 163 164 if (info != 0) 165 fprintf(stderr, "failure with error %d\n", info); 165 166 // for (i=0; i<n; ++i) printf("%lf\n", y[i]); 166 167 … … 180 181 static double malloc_times = 0.0; 181 182 static double malloc_total = 0.0; 182 static int malloc_amount[] = { 0, 32, 128, 256, 1024, 4096, 16384, 65536, 262144, COIN_INT_MAX};183 static int malloc_amount[] = { 0, 32, 128, 256, 1024, 4096, 16384, 65536, 262144, COIN_INT_MAX }; 183 184 static int malloc_n = 10; 184 double malloc_counts[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};185 double malloc_counts[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 185 186 bool malloc_counts_on = true; 186 void * operator new (size_t size) throw (std::bad_alloc) 187 { 188 malloc_times ++; 189 malloc_total += size; 190 int i; 191 for (i = 0; i < malloc_n; i++) { 192 if ((int) size <= malloc_amount[i]) { 193 malloc_counts[i]++; 194 break; 195 } 187 void *operator new(size_t size) throw(std::bad_alloc) 188 { 189 malloc_times++; 190 malloc_total += size; 191 int i; 192 for (i = 0; i < malloc_n; i++) { 193 if ((int)size <= malloc_amount[i]) { 194 malloc_counts[i]++; 195 break; 196 196 } 197 if (size>100000) { 198 printf("allocating %ld bytes\n",size); 199 } 200 void * p = malloc(size); 201 return p; 202 } 203 void operator delete (void *p) throw() 204 { 205 free(p); 197 } 198 if (size > 100000) { 199 printf("allocating %ld bytes\n", size); 200 } 201 void *p = malloc(size); 202 return p; 203 } 204 void operator delete(void *p) throw() 205 { 206 free(p); 206 207 } 207 208 static void malloc_stats2() 208 209 { 209 210 211 212 213 214 215 216 217 218 } 219 #endif 210 double average = malloc_total / malloc_times; 211 printf("count %g bytes %g  average %g\n", malloc_times, malloc_total, average); 212 for (int i = 0; i < malloc_n; i++) 213 printf("%g ", malloc_counts[i]); 214 printf("\n"); 215 malloc_times = 0.0; 216 malloc_total = 0.0; 217 memset(malloc_counts, 0, sizeof(malloc_counts)); 218 // print results 219 } 220 #endif //CLP_MALLOC_STATISTICS 220 221 int 221 222 #if defined(_MSC_VER) 222 __cdecl223 __cdecl 223 224 #endif // _MSC_VER 224 main(int argc, const char *argv[])225 { 226 #ifdef CILK_TEST 225 main(int argc, const char *argv[]) 226 { 227 #ifdef CILK_TEST 227 228 cilkTest(); 228 #endif 229 #endif 229 230 #if CLP_USE_OPENBLAS 230 231 openblas_set_num_threads(CLP_USE_OPENBLAS); 231 #endif 232 #endif 232 233 #ifdef LAPACK_TEST 233 234 //void openblas_set_num_threads(int num_threads); 234 235 openblas_set_num_threads(1); 235 if (argc<2){236 if (argc < 2) { 236 237 printf("Error  need size of matrix for lapack test\n"); 237 return 1; 238 } 239 int n =atoi(argv[1]);240 printf("n=%d\n", n);241 if (argc>2){242 int nThreads =atoi(argv[2]);243 printf("Using %d threads\n", nThreads);238 return 1; 239 } 240 int n = atoi(argv[1]); 241 printf("n=%d\n", n); 242 if (argc > 2) { 243 int nThreads = atoi(argv[2]); 244 printf("Using %d threads\n", nThreads); 244 245 openblas_set_num_threads(nThreads); 245 246 } … … 248 249 #endif 249 250 #ifndef ABC_INHERIT 250 ClpSimplex * 251 ClpSimplex *models = new ClpSimplex[1]; 251 252 #else 252 AbcSimplex * 253 AbcSimplex *models = new AbcSimplex[1]; 253 254 #endif 254 255 std::cout << "Coin LP version " << CLP_VERSION 255 256 << ", build " << __DATE__ << std::endl; 256 257 // Print command line 257 258 if (argc > 1) { … … 262 263 } 263 264 ClpMain0(models); 264 int returnCode = ClpMain1(argc, argv, models);265 delete 265 int returnCode = ClpMain1(argc, argv, models); 266 delete[] models; 266 267 return returnCode; 267 268 } … … 323 324 double dowork(double i) 324 325 { 325 326 327 328 329 330 331 332 333 } 334 static void doSomeWork(double * a,int low, int high)335 { 336 if (high low>300) {337 int mid =(high+low)>>1;338 cilk_spawn doSomeWork(a, low,mid);339 doSomeWork(a, mid,high);326 // Waste time: 327 int j; 328 double k = i; 329 for (j = 0; j < 50000; ++j) { 330 k += k / ((j + 1) * (k + 1)); 331 } 332 333 return k; 334 } 335 static void doSomeWork(double *a, int low, int high) 336 { 337 if (high  low > 300) { 338 int mid = (high + low) >> 1; 339 cilk_spawn doSomeWork(a, low, mid); 340 doSomeWork(a, mid, high); 340 341 cilk_sync; 341 342 } else { 342 for (int i = low; i < high; ++i) {343 for (int i = low; i < high; ++i) { 343 344 a[i] = dowork(a[i]); 344 345 } … … 348 349 void cilkTest() 349 350 { 350 unsigned int n = 10000; 351 //cilk::cilkview cv; 352 353 354 double* a = new double[n]; 355 356 for(unsigned int i = 0; i < n; i++) { 357 // Populate A 358 a[i] = (double) ((i * i) % 1024 + 512) / 512; 351 unsigned int n = 10000; 352 //cilk::cilkview cv; 353 354 double *a = new double[n]; 355 356 for (unsigned int i = 0; i < n; i++) { 357 // Populate A 358 a[i] = (double)((i * i) % 1024 + 512) / 512; 359 } 360 361 std::cout << "Iterating over " << n << " integers" << std::endl; 362 363 //cv.start(); 364 #if 1 365 //#pragma cilk_grainsize=CILK_FOR_GRAINSIZE 366 cilk_for(unsigned int i = 0; i < n; ++i) 367 { 368 a[i] = dowork(a[i]); 369 } 370 #else 371 doSomeWork(a, 0, n); 372 #endif 373 int *which = new int[n]; 374 unsigned int n2 = n >> 1; 375 for (int i = 0; i < n2; i++) 376 which[i] = n  2 * i; 377 cilk::reducer_max_index< int, double > maximumIndex(1, 0.0); 378 cilk_for(unsigned int i = 0; i < n2; ++i) 379 { 380 int iWhich = which[i]; 381 maximumIndex.calc_max(iWhich, a[iWhich]); 382 } 383 int bestIndex = maximumIndex.get_index(); 384 int bestIndex2 = 1; 385 double largest = 0.0; 386 cilk_for(unsigned int i = 0; i < n2; ++i) 387 { 388 int iWhich = which[i]; 389 if (a[iWhich] > largest) { 390 bestIndex2 = iWhich; 391 largest = a[iWhich]; 359 392 } 360 361 std::cout << "Iterating over " << n << " integers" << std::endl; 362 363 //cv.start(); 364 #if 1 365 //#pragma cilk_grainsize=CILK_FOR_GRAINSIZE 366 cilk_for(unsigned int i = 0; i < n; ++i) { 367 a[i] = dowork(a[i]); 368 } 369 #else 370 doSomeWork(a,0,n); 371 #endif 372 int * which =new int[n]; 373 unsigned int n2=n>>1; 374 for (int i=0;i<n2;i++) 375 which[i]=n2*i; 376 cilk::reducer_max_index<int,double> maximumIndex(1,0.0); 377 cilk_for(unsigned int i = 0; i < n2; ++i) { 378 int iWhich=which[i]; 379 maximumIndex.calc_max(iWhich,a[iWhich]); 380 } 381 int bestIndex=maximumIndex.get_index(); 382 int bestIndex2=1; 383 double largest=0.0; 384 cilk_for(unsigned int i = 0; i < n2; ++i) { 385 int iWhich=which[i]; 386 if (a[iWhich]>largest) { 387 bestIndex2=iWhich; 388 largest=a[iWhich]; 389 } 390 } 391 assert (bestIndex==bestIndex2); 392 //cv.stop(); 393 //cv.dump("cilkforresults", false); 394 395 //std::cout << cv.accumulated_milliseconds() / 1000.f << " seconds" << std::endl; 396 397 exit(0); 398 } 399 #endif 393 } 394 assert(bestIndex == bestIndex2); 395 //cv.stop(); 396 //cv.dump("cilkforresults", false); 397 398 //std::cout << cv.accumulated_milliseconds() / 1000.f << " seconds" << std::endl; 399 400 exit(0); 401 } 402 #endif 403 404 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 405 */
Note: See TracChangeset
for help on using the changeset viewer.