dilation(unsigned char **image_pixel,int height,int width) { int i,j; unsigned char **tmppixel; tmppixel=(unsigned char**)mymalloc2(height,width,sizeof(unsigned char)); for(j=0;j<height;j++) for(i=0;i<width;i++) tmppixel[j][i]=image_pixel[j][i]; for(j=1;j<height-1;j++) for(i=1;i<width-1;i++){ if(tmppixel[j-1][i-1]==LOW || tmppixel[j-1][i ]==LOW || tmppixel[j-1][i+1]==LOW || tmppixel[j ][i-1]==LOW || tmppixel[j ][i+1]==LOW || tmppixel[j+1][i-1]==LOW || tmppixel[j+1][i ]==LOW || tmppixel[j+1][i+1]==LOW ) image_pixel[j][i]=LOW; } myfree2((void **)tmppixel,height); }
int main(int argc, char *argv[]) { int n=get_num_ind(); int i,j; struct timeval tv1,tv2; adouble *xad; adouble fad; double f; double *x; x=new double[n]; xad=new adouble[n]; get_initial_value(x); printf("evaluating the function..."); trace_on(tag); for(i=0;i<n;i++) { xad[i] <<= x[i]; } fad=func_eval(xad); fad >>= f; trace_off(); printf("done!\n"); // printf("function value =<%10.20f>\n",f); // function(tag,1,n,x,&f); // printf("adolc func value=<%10.20f>\n",f); //tape_doc(tag,1,n,x,&f); #ifdef _compare_with_full double **H; H = myalloc2(n,n); printf("computing full hessain...."); gettimeofday(&tv1,NULL); hessian(tag,n,x,H); printf("done\n"); gettimeofday(&tv2,NULL); printf("Computing the full hessian cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #ifdef _PRINTOUT for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf("H[%d][%d]=<%10.10f>",i,j,H[i][j]); } printf("\n"); } printf("\n"); #endif #endif #ifdef edge_pushing unsigned int *rind = NULL; unsigned int *cind = NULL; double *values = NULL; int nnz; int options[2]; options[0]=PRE_ACC; options[1]=COMPUT_GRAPH; gettimeofday(&tv1,NULL); // edge_hess(tag, 1, n, x, &nnz, &rind, &cind, &values, options); sparse_hess(tag,n,0,x, &nnz, &rind, &cind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: edge pushing cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #ifdef _PRINTOUT for(i=0;i<nnz;i++){ printf("<%d,%d>:<%10.10f>\n",cind[i],rind[i],values[i]); // printf("%d %d \n", rind[i], cind[i]); } #endif #endif #ifdef _compare_with_full #ifdef edge_pushing compare_matrix(n,H,nnz,cind,rind,values); #endif myfree2(H); #endif #ifdef edge_pushing printf("nnz=%d\n", nnz); free(rind); rind=NULL; free(cind); cind=NULL; free(values); values=NULL; #endif delete[] x; delete[] xad; return 0; }
int main(int argc, char *argv[]) { int n=NUM_IND; int i,j; struct timeval tv1,tv2; adouble *xad; adouble fad; double f; double *x; x=new double[n]; xad=new adouble[n]; get_initials(x, n); // printf("evaluating the function..."); trace_on(tag); for(i=0;i<n;i++) { xad[i] <<= x[i]; } fad=eval_func<adouble>(xad, n); fad >>= f; trace_off(); // printf("done!\n"); std::cout << "y = " << f << std::endl; #ifdef COMPARE_WITH_FULL_HESS double **H; H = myalloc2(n,n); printf("computing full hessain...."); gettimeofday(&tv1,NULL); hessian(tag,n,x,H); printf("done\n"); gettimeofday(&tv2,NULL); printf("Computing the full hessian cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #ifdef PRINT_RESULTS for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf("H[%d][%d]=<%10.10f>",i,j,H[i][j]); } printf("\n"); } printf("\n"); #endif #endif unsigned int *rind = NULL; unsigned int *cind = NULL; double *values = NULL; int nnz; int options[2]; #ifdef LIVARH options[0]=0; options[1]=1; gettimeofday(&tv1,NULL); edge_hess(tag, 1, n, x, &nnz, &rind, &cind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: LivarH cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #endif #ifdef LIVARHACC options[0]=1; options[1]=1; gettimeofday(&tv1,NULL); edge_hess(tag, 1, n, x, &nnz, &rind, &cind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: LivarHACC cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #endif // Sparse ADOL-C drivers report the upper matrix #ifdef DIRECT options[0]=0; options[1]=1; gettimeofday(&tv1,NULL); sparse_hess(tag, n, 0, x, &nnz, &cind, &rind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: direct recovery cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #endif #ifdef INDIRECT options[0]=0; options[1]=0; gettimeofday(&tv1,NULL); sparse_hess(tag, n, 0, x, &nnz, &cind, &rind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: indirect recovery cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #endif #ifdef PRINT_RESULTS for(i=0;i<nnz;i++){ printf("<%d,%d>:<%10.10f>\n",rind[i],cind[i],values[i]); } #endif #ifdef COMPARE_WITH_FULL_HESS compare_matrix(n,H,nnz,rind,cind,values); myfree2(H); #endif free(rind); rind=NULL; free(cind); cind=NULL; free(values); values=NULL; delete[] x; delete[] xad; return 0; }
double evaluate_derivatives(int n, int m, double* x, int* options) { int order = options[0]; int nnz; double t1 = k_getTime(); if (options[1] == 0) { // Teed = new double*[n]; assert(m == 1); double** seed = new double*[n]; for (int i = 0; i < n; i++) { seed[i] = new double[n]; for (int j = 0; j < n; j++) { seed[i][j] = ((i==j)?1.0:0.0); } } int dim = binomi(n+order, order); double** tensorhelp = myalloc2(1, dim); tensor_eval(TAG, 1, n, order, n, x, tensorhelp, seed); for (int i = 0; i < n; i++) { delete[] seed[i]; } delete[] seed; myfree2(tensorhelp); } else { if (order == 2) { // Hessian assert(m == 1); if (options[1] == 1 || options[1] == 2) { // Direct or Indirect int opt[2] = {0, 0}; // default is indirect; if (options[1] == 1) {opt[0] = 1;} // set direct; unsigned int * rind = NULL; unsigned int * cind = NULL; double * values = NULL; sparse_hess(TAG, n, 0, x, &nnz, &rind, &cind, &values, opt); #ifdef PRINT_RESULT for (int i = 0; i < nnz; i++) { printf("H[%d, %d] = %.6f\n", rind[i], cind[i], values[i]); } #endif free(rind); free(cind); free(values); } else if (options[1] == 3) { // FullHess double** H = new double*[n]; for (int i = 0; i < n; i++) { H[i] = new double[n]; } hessian(TAG, n, x, H); nnz = n*n; #ifdef PRINT_RESULT for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("H[%d, %d] = %.6f\n", i, j, H[i][j]); } } #endif for (int i = 0; i < n; i++) { delete[] H[i]; } delete[] H; } else if (options[1] == 4) { // Single Hv double v[n]; double Hv[n]; for (int i = 0; i < n; i++) { v[i] = 1.0; Hv[i] = 0.0; } hess_vec(TAG, n, x, v, Hv); nnz = n; } else if (options[1] == 5) { // dense second order reverse double** H = new double*[n]; for (int i = 0; i < n; i++) { H[i] = new double[n]; } hessian_dense(TAG, n, x, H); nnz = n*n; #ifdef PRINT_RESULT for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("H[%d, %d] = %.6f\n", i, j, H[i][j]); } } #endif for (int i = 0; i < n; i++) { delete[] H[i]; } delete[] H; } else if (options[1] == 6){ // sparse second order reverse unsigned int * rind = NULL; unsigned int * cind = NULL; double * values = NULL; hessian_sparse(TAG, n, x, &nnz, &rind, &cind, &values); #ifdef PRINT_RESULT for (int i = 0; i < nnz; i++) { printf("H[%d, %d] = %.6f\n", rind[i], cind[i], values[i]); } #endif free(rind); free(cind); free(values); } else if (options[1] == 7) { // Hess-matrix options double** H = myalloc2(n, n); double y; double*** Xppp = myalloc3(n, n, 1); double*** Yppp = myalloc3(1, n, 1); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Xppp[i][j][0] = 0; } Xppp[i][i][0] = 1.0; } double** Upp = myalloc2(1,2); Upp[0][0] = 1; Upp[0][1] = 0; double*** Zppp = myalloc3(n, n, 2); int ret_val = hov_wk_forward(TAG,1,n,1,2,n,x,Xppp,&y,Yppp); ret_val = hos_ov_reverse(TAG,1,n,1,n,Upp,Zppp); for (int i = 0; i < n; ++i) { for (int l = 0; l < n; ++l) { H[l][i] = Zppp[i][l][1]; } } #ifdef PRINT_RESULT for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("H[%d, %d] = %.6f\n", i, j, H[i][j]); } } #endif myfree2(H); myfree3(Xppp); myfree3(Yppp); myfree2(Upp); myfree3(Zppp); } } else if (order == 1) { // Gradient or Jacobian if (m == 1) { // gradient double g[n]; gradient(TAG, n, x, g); #ifdef PRINT_RESULT for (int i = 0; i < n; i++) { printf("g[%d] = %.6f\n", i, g[i]); } #endif } else { // jacobian double** J = new double*[m]; for (int i = 0; i < m; i++) { J[i] = new double[n]; } jacobian(TAG, m, n, x, J); #ifdef PRINT_RESULT for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("J[%d][%d] = %.6f\n", i, j, J[i][j]); } } #endif for (int i = 0; i < m; i++) { delete[] J[i]; } delete[] J; } nnz = n*m; } } double time_elapsed = k_getTime() - t1; size_t size; size_t** tind; double* values; printf("ADOLC nnz[%d] method[%d] order[%d] timing = %.6f\n", nnz, options[1], options[0], time_elapsed); return time_elapsed; }
BEGIN_C_DECLS /****************************************************************************/ /* DRIVERS FOR ODEs */ /*--------------------------------------------------------------------------*/ /* forodec */ /* forodec(tag, n, tau, dold, dnew, X[n][d+1]) */ int forodec(short tag, /* tape identifier */ int n, /* space dimension */ double tau, /* scaling defaults to 1.0 */ int dol, /* previous degree defaults to zero */ int deg, /* New degree of consistency */ double** Y) /* Taylor series */ { /********************************************************************* This is assumed to be the autonomous case. Here we are just going around computing the vectors y[][j] for dol < j <= deg by successive calls to forward that works on the tape identified by tag. This tape (array of file) must obviously have been generated by a the execution of an active section between trace_on and trace_off with n independent and n dependent variables y must have been set up as pointer to an array of n pointers to double arrays containing at least deg+1 components. The scaling by tau is sometimes necessary to avoid overflow. **********************************************************************/ int rc= 3; int i, j, k; double taut; ADOLC_OPENMP_THREAD_NUMBER; ADOLC_OPENMP_GET_THREAD_NUMBER; if ( n > ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_nax || deg > ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_dax ) { if (ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_nax) { myfree1(ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_y); myfree1(ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_z); myfree2(ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_Z); } ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_Z = myalloc2(n, deg); ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_z = myalloc1(n); ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_y = myalloc1(n); ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_nax = n; ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_dax = deg; } for (i = 0; i < n; ++i) { ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_y[i] = Y[i][0]; /*printf("y[%i] = %f\n",i,y[i]);*/ for (k = 0; k < deg; ++k) { Y[i][k] = Y[i][k+1]; /*printf("Y[%i][%i] = %f\n",i,k,Y[i][k]);*/ } } /****** Here we get going ********/ if (dol == 0) { j = dol; /* j = 0 */ k = (deg) * (j == deg-1 ) ; /* keep death values in prepration */ MINDEC(rc, zos_forward(tag, n, n, k, ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_y, ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_z)); /* for reverse called by jacode */ if(rc < 0) return rc; taut = tau / (1 + j); /* only the last time through. */ for (i = 0; i < n; ++i) Y[i][j] = taut * ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_z[i]; dol++; /* !!! */ } for (j = dol; j < deg; ++j) { k = (deg)*(j == deg-1) ; /* keep death values in prepration */ MINDEC(rc, hos_forward(tag, n, n, j, k, ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_y, Y, ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_z, ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_Z)); /* for reverse called by jacode */ if( rc < 0) return rc; taut = tau / (1 + j); /* only the last time through. */ for (i = 0; i < n; ++i) Y[i][j] = taut * ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_Z[i][j-1]; } /****** Done ********/ for (i = 0; i < n; ++i) { for (k = deg; k > 0; --k) { Y[i][k] = Y[i][k-1]; /*printf("Y[%i][%i] = %f\n",i,k,Y[i][k]);*/ } Y[i][0] = ADOLC_CURRENT_TAPE_INFOS.pTapeInfos.forodec_y[i]; /*printf("Y[%i][0] = %f\n",i,Y[i][0]);*/ } return rc; }