示例#1
0
// Main function
// *********************************************************************
int main(int argc, char **argv)
{
    
        
    if (!device_comm_queue)
        ocl_initialize();

    const char* program_source;
    
    program_source = load_source_file(argv[1]);

    
    cl_program ocl_program = clCreateProgramWithSource(device_context, 1,
        (const char**)&program_source, NULL, NULL);
    printf("********** program created.\n");


    
    // Build the program (OpenCL JIT compilation)
    char options[100];
    const char* flags = "-g -w -I\"";
    const char* OMHOME = getenv("OPENMODELICAHOME");
    const char* OMINCL = "/include/omc\"";
    const char* OMBIN = "/bin\"";
    
    if ( OMHOME != NULL )
    {
        strcpy(options, flags);
        strcat(options, OMHOME);
        strcat(options, OMINCL);
        strcat(options, " -I\"");
        strcat(options, OMHOME);
        strcat(options, OMBIN);
        printf("Building OpenCL code with flags %s\n",options);
        cl_int err;
        err = clBuildProgram(ocl_program, 0, NULL, options, NULL, NULL);
        ocl_error_check(OCL_BUILD_PROGRAM, err);
    
        size_t size;
        clGetProgramBuildInfo(ocl_program, ocl_device, CL_PROGRAM_BUILD_LOG,        // Get build log size
                                  0, NULL, &size);
        char * log = (char*)malloc(size);
        clGetProgramBuildInfo(ocl_program,ocl_device,CL_PROGRAM_BUILD_LOG,size,log, NULL);
        printf("\t\tCL_PROGRAM_BUILD_LOG:  \t%s\n", log);
        free(log);
        
        if(err){
            printf("Errors detected in compilation of OpenCL code:\n");
            exit(1);
        }
        else
            printf("Program built successfuly.\n");
        
        //if no error create the binary
        clGetProgramInfo(ocl_program, CL_PROGRAM_BINARY_SIZES, 
        sizeof(size_t), &size, NULL);
        unsigned char * binary = (unsigned char*)malloc(size);
        printf("Size of program binary :\t%d\n",size);
        clGetProgramInfo(ocl_program, CL_PROGRAM_BINARIES, sizeof(size_t), &binary, NULL);
        printf("Program binary retrived.\n");        
        
        const char* binary_ext = ".bin";
        char* binary_name = strcat(argv[1],binary_ext);
        printf("binary file name %s\n", binary_name);
        FILE * cache;
        cache = fopen(binary_name, "wb");
        fwrite(binary, sizeof(char), size, cache);
        fclose(cache);
        //free(binary);
        
        
        
        err = 0;
        cl_program newprogram = clCreateProgramWithBinary(device_context, 1, &ocl_device, &size, (const unsigned char **)&binary, NULL, &err);
        if(!err)
            printf("Program created from binary\n");
        else{
            switch (err){
                case CL_INVALID_CONTEXT:
                    printf("Error building program:\n");
                    printf("CL_INVALID_CONTEXT \n");
                    break;
                case CL_INVALID_VALUE:
                    printf("Error building program:\n");
                    printf("CL_INVALID_VALUE \n");
                    break;
                case CL_INVALID_DEVICE:
                    printf("Error building program:\n");
                    printf("CL_INVALID_DEVICE \n");
                    break;
                case CL_INVALID_BINARY:
                    printf("Error building program:\n");
                    printf("CL_INVALID_BINARY \n");
                    break;
                case CL_OUT_OF_HOST_MEMORY:
                    printf("Error building program:\n");
                    printf("CL_OUT_OF_HOST_MEMORY \n");
                    break;
            }
        }
                
        
            
            
        return 0;

   }
   else
   {
       printf("Couldn't find OPENMODELICAHOME!\n");
       exit(1);
   }

    

    
    ocl_clean_up();

    
    return 0;
}
void setup_cl_compute() {
	cl_int error;
	// Load the program source
        char* program_text = load_source_file("kernel.cl");
        if (program_text == NULL) {
          printf("Failed to load source file.\n");
          exit(-1);
        }

        // Create the program

        cl_program program;
        start_perf_measurement(&program_perf);
        program = clCreateProgramWithSource(opencl_context, 1, (const char**)&program_text, NULL, &error);
        checkError(error, "clCreateProgramWithSource");

        // Compile the program and check for errors
        error = clBuildProgram(program, 1, &opencl_device, NULL, NULL, NULL);
        // Get the build errors if there were any
        if (error != CL_SUCCESS) {
          printf("clCreateProgramWithSource failed (%d). Getting program build log.\n", error);
          cl_int error2;
          char build_log[10000];
          error2 = clGetProgramBuildInfo(program, opencl_device, CL_PROGRAM_BUILD_LOG, 10000, build_log, NULL);
          checkError(error2, "clGetProgramBuildInfo");
          printf("Build Failed. Log:\n%s\n", build_log);
        }
        checkError(error, "clBuildProgram");

        // Create the computation kernel
        update_kernel = clCreateKernel(program, "update", &error);
        checkError(error, "clCreateKernel");
        stop_perf_measurement(&program_perf);

	// Create the data objects
        //cl_mem in_buffer, out_buffer;
        start_perf_measurement(&create_perf);
        in_buffer = clCreateBuffer(opencl_context, CL_MEM_READ_ONLY, SIZE_BYTES, NULL, &error);
        clFinish(opencl_queue);
        stop_perf_measurement(&create_perf);

        checkError(error, "clCreateBuffer");
        out_buffer = clCreateBuffer(opencl_context, CL_MEM_WRITE_ONLY, SIZE_BYTES, NULL, &error);
        checkError(error, "clCreateBuffer");
	
	// Set the kernel arguments
        /*start_perf_measurement(&update_perf);
        error = clSetKernelArg(kernel, 0, sizeof(in_buffer), &in_buffer);
        checkError(error, "clSetKernelArg in");
        error = clSetKernelArg(kernel, 1, sizeof(out_buffer), &out_buffer);
        checkError(error, "clSetKernelArg out");
        stop_perf_measurement(&update_perf);

        // Enqueue the kernel
        size_t global_dimensions[] = {SIZE,SIZE,0}; // Ignore the border

        start_perf_measurement(&update_perf);
        error = clEnqueueNDRangeKernel(opencl_queue, kernel, 2, NULL, global_dimensions, NULL, 0, NULL, NULL);
        clFinish(opencl_queue);
        stop_perf_measurement(&update_perf);

        checkError(error, "clEnqueueNDRangeKernel");*/

}
示例#3
0
int main(int argc, char **argv)
{
	FILE *fsource, *fdump;
	struct Region *region, *aRegion;
	char dumpfile[256], *directory, buf[32];

	struct Hashtable traces, routes;
	struct Item *aItem, *tItem, *bItem;
	struct Trace *aTrace, *bTrace, *dTrace;
	struct Busroute *aRoute;
	struct Duallist roads;

	double wdist=WEIGHT_DISTANCE_ERROR,  wangle = WEIGHT_ANGLE_ERROR, wlength = WEIGHT_LENGTH, wturns = WEIGHT_TURNS;

	unsigned long i;
	int defact = 0;
	char *defactd;


	if(argc < 2) {
	      printf("Usage: %s [-d directory] [-o defact_dir] [-w wdist wangle wlength wturns] .map (.ogd|.lst ...)\n", argv[0]);
	      exit(1);
	}

	directory = ".";
	defactd = "./defect";
	while(argv[1][0] == '-') {
	      switch(argv[1][1]) {
	      case 'o':
		      defact = 1;
		      defactd = argv[2];
		      argc-=2;
		      argv+=2;
		      break;

	      case 'd':
		      directory = argv[2];
		      argc-=2;
		      argv+=2;
		      break;
	      case 'w':
		      wdist = atof(argv[2]);
		      wangle = atof(argv[3]);
		      wlength = atof(argv[4]);
		      wturns = atof(argv[5]);
		      argc -= 5;
		      argv += 5;
		      break;
	      default:
	      	      printf("Usage: %s [-d directory] [-o defact_dir] [-w wdist wangle wlength wturns] .map (.ogd|.lst ...)\n", argv[0]);
	      }
	}

	region = NULL;
	if((fsource=fopen(argv[1], "rb"))!=NULL) {
	      region = region_load_func(fsource, NULL, -1, -1);
	      fclose(fsource);
	      argc--;
	      argv++;
	}

	hashtable_init(&traces, 2000, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))trace_has_name);
	hashtable_init(&routes, 100, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))route_has_name);
	while(region && argc > 1) {
		if((fsource=fopen(argv[1], "r"))!=NULL) {
			printf("Loading %s file ...\n", argv[1]);
			load_source_file(fsource, region, &traces, (void*(*)(int, FILE*, struct Region *, void *, time_t *, time_t *, struct Box *))load_trace_with_hashtable, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, &routes, (void*(*)(FILE*, struct Region*, void *))load_route_with_hashtable, NULL, NULL, NULL);
			fclose(fsource);
		}

		argc--;
		argv++;
	}


	if(traces.count != 0) {
	      for (i = 0; i<traces.size; i++) {
		      aItem = traces.head[i];
		      while(aItem != NULL ) {
				aTrace = (struct Trace*)aItem->datap;
				aRegion = NULL;
				bTrace = NULL;
				dTrace = NULL;

				if((aTrace->type == FILE_ORIGINAL_GPS_BUS|| aTrace->type == FILE_MODIFIED_GPS_BUS) && routes.count) {
					duallist_init(&roads);
					sprintf(buf, "%s_upway", aTrace->onRoute);
  					tItem = hashtable_find(&routes, buf);
					if(tItem) {
						aRoute = (struct Busroute*)tItem->datap;
						bItem = aRoute->path->roads.head;
						while(bItem) {
							duallist_add_to_tail(&roads, bItem->datap);
							bItem = bItem->next;
						}	
					}
					sprintf(buf, "%s_downway", aTrace->onRoute);
  					tItem = hashtable_find(&routes, buf);
					if(tItem) {
						aRoute = (struct Busroute*)tItem->datap;
						bItem = aRoute->path->roads.head;
						while(bItem) {
							duallist_add_to_tail(&roads, bItem->datap);
							bItem = bItem->next;
						}	
					}

					aRegion = build_region_with_roads(&roads);
					if(aRegion) {
		 				amend_trace(aRegion, aTrace, wdist, wangle, wlength, wturns, &bTrace, &dTrace);
						region_free_func(aRegion);
					}
					duallist_destroy(&roads, NULL);

				} else { 
		 			amend_trace(region, aTrace, wdist, wangle, wlength, wturns, &bTrace, &dTrace); // entrance of the mapmatching code
				}

#ifdef DEBUG
                sprintf(dumpfile, "transVecDump_%s.txt",aTrace->vName);
				if( (fdump = fopen(dumpfile, "w"))!=NULL) {
					trace_dump_transVecHistory(fdump, aTrace);
					fflush(fdump);
					fclose(fdump);
				}
				
#endif

				if(bTrace) {
					mkdir(directory,0777);
					sprintf(dumpfile, "%s/%s.mgd", directory, aTrace->vName);
					if( (fdump = fopen(dumpfile, "w"))!=NULL) {
						trace_dump_func(fdump, bTrace);
						fflush(fdump);
						fclose(fdump);
					}
					trace_free_func(bTrace);
				}

				if(dTrace) {
					if(defact && dTrace->reports.nItems) {
						mkdir(defactd,0777);
						sprintf(dumpfile, "%s/%s.ogd", defactd, aTrace->vName);
						if( (fdump = fopen(dumpfile, "w"))!=NULL) {
							trace_dump_func(fdump, dTrace);
							fflush(fdump);
							fclose(fdump);
						}
					}
					trace_free_func(dTrace);
				}
				trace_free_func((struct Trace*)hashtable_pick(&traces, aTrace->vName));

			      	aItem = aItem->next;
		      }
	      }
	} 
	 
	hashtable_destroy(&traces, (void(*)(void*))trace_free_func);
	hashtable_destroy(&routes, (void(*)(void*))route_free_func);
	region_free_func(region);
	return 0;
}
示例#4
0
void ocl_build_p_from_src(){

    // Create OpenCL program with source code

    const char* program_source;


    program_source = load_source_file(omc_ocl_kernels_source);


#if BE_OCL_VERBOSE
    printf("--- Creating OpenCL program");
#endif
    // omc_ocl_program declared in omc_ocl_util.h
    omc_ocl_program = clCreateProgramWithSource(device_context, 1,
        (const char**)&program_source, NULL, NULL);

#if BE_OCL_VERBOSE
    printf("\t\t\t - OK.\n");
#endif

    free((void*)program_source);



    // Check for OpenModelica env variable.
    const char* OMHOME = getenv("OPENMODELICAHOME");
    if ( OMHOME == NULL )
    {
       printf("Couldn't find OPENMODELICAHOME!\n");
       exit(1);
    }


    // Build the program (OpenCL JIT compilation).
#if BE_OCL_VERBOSE
    printf("--- Building OpenCL program \n");
#endif

    char options[100];
    const char* flags = "-I\"";
    const char* OMEXT = "/include/omc/c/\"";


    strcpy(options, flags);
    strcat(options, OMHOME);
    strcat(options, OMEXT);

#if BE_OCL_VERBOSE
    printf("\t :Using flags %s\n",options);
#endif

    // Build the OpenCL program.
    cl_int err = 0;
    err = clBuildProgram(omc_ocl_program, 0, NULL, options, NULL, NULL);
    ocl_error_check(OCL_BUILD_PROGRAM, err);

    // Get build log size.
    size_t size;
    clGetProgramBuildInfo(omc_ocl_program, ocl_device, CL_PROGRAM_BUILD_LOG,
                              0, NULL, &size);

    // Get the build log.
    char * log = (char*)malloc(size);
    clGetProgramBuildInfo(omc_ocl_program,ocl_device,CL_PROGRAM_BUILD_LOG,size,log, NULL);

    if(err){
        printf("Build failed: Errors detected in compilation of OpenCL code:\n");
        printf("CL_PROGRAM_BUILD_LOG:  \n%s\n", log);
        free(log);
        exit(1);
    }

    free(log);

}
示例#5
0
int main(int argc, char **argv)
{
	FILE *fsource=NULL;
	struct Region *region;
	struct Hashtable routes;
	long routeTableSize = 500;
	struct Cell *aCell;
	unsigned long i, j, count;
	int cellsize = 300;


	if(argc < 3) {
	      printf("%s is used to get the coverage of bus routes.\n", argv[0]);
	      printf("Usage: %s [-cell size] .map (.lst |.bus ...)\n", argv[0]);
	      exit(1);
	}

	while(argv[1][0] == '-') {
		switch(argv[1][1]) {
		case 'c':
			cellsize = atoi(argv[2]);
			argc-=2;
			argv+=2;
			break;
		default:
	      		printf("Usage: %s [-cell size] .map (.lst |.bus ...)\n", argv[0]);
	      }
	}

	region = NULL;
	if((fsource=fopen(argv[1], "rb"))!=NULL) {
	      region = region_load_func(fsource, NULL, cellsize, -1);
	      fclose(fsource);
	      argc--;
	      argv++;
	}

	hashtable_init(&routes, routeTableSize, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))route_has_name);
	while(argc > 1) {
		if((fsource=fopen(argv[1], "r"))!=NULL) {
			load_source_file(fsource, region, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &routes, (void*(*)(FILE*, struct Region*, void *))load_route_with_hashtable, NULL, NULL, NULL);
			fclose(fsource);
		}

		argc--;
		argv++;
	}

	count = 0;
	for(i = 0; i<region->hCells; i++)
		for(j = 0; j<region->vCells;j++) {
			aCell = region->mesh + i*region->vCells + j;
			if(is_cell_in_polygon(aCell, region->chosen_polygon))
				count ++;
		}

	setup_cells_with_routes(region, &routes);
	printf("%d %.2lf\n", cellsize, ((double)region->busCoveredCells.nItems)/count);

	hashtable_destroy(&routes, (void(*)(void*))route_free_func);
	region_free_func(region);
	return 0;
}
示例#6
0
int main(int argc, char **argv)
{
	FILE *fsource, *fdump;
	struct Region *region, *aRegion;
	int insertMode = INSERT_MODE_AVGSPEED;
	int outputMode = OUTPUT_MODE_CELL;
	int interval;
	char dumpfile[256], *directory, buf[32];

	struct Hashtable traces, routes;
	struct Item *aItem, *bItem, *tItem;
	struct Trace *aTrace, *bTrace;
	unsigned long i;
	struct Duallist roads;
	struct Busroute *aRoute;

	if(argc < 3) {
		printf("%s is used to retrieve required spots of vehicles from mgd files. New GPS reports are generated according to the given model.\n", argv[0]);
	      	printf("Usage: %s [-insert avgspeed|traffic] [-output cell|interval seconds] [-d directory] .map (.mgd|.lst ...)\n", argv[0]);
	      	exit(1);
	}

	directory = ".";
	while(argv[1][0] == '-') {
	      switch(argv[1][1]) {
	      case 'i':
		      if(strcmp(argv[2], "traffic")==0)
			  insertMode = INSERT_MODE_TRAFFIC;
		      else if(strcmp(argv[2], "avgspeed")==0)
			  insertMode = INSERT_MODE_AVGSPEED;
		      argc-=2;
		      argv+=2;
		      break;
	      case 'd':
		      directory = argv[2];
		      argc-=2;
		      argv+=2;
		      break;

	      case 'o':
		      if(argv[2][0]=='c') {
			      outputMode = OUTPUT_MODE_CELL;
			      argc-=2;
			      argv+=2;
		      } else if(argv[2][0] == 'i') {
			      outputMode = OUTPUT_MODE_INTERVAL;
			      interval = atoi(argv[3]);
			      argc-=3;
			      argv+=3;
		      }
		      break;

	      default:
	              printf("Usage: %s [-mode avgspeed|traffic] [-cell] [-interval seconds] .map (.mgd|.lst ...)\n", argv[0]);
	      }
	}

	region = NULL;
	if((fsource=fopen(argv[1], "rb"))!=NULL) {
	      region = region_load_func(fsource, NULL, -1, -1);
	      fclose(fsource);
	      argc--;
	      argv++;
	}

	hashtable_init(&traces, 2000, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))trace_has_name);
	hashtable_init(&routes, 100, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))route_has_name);
	while(region && argc > 1) {
		if((fsource=fopen(argv[1], "r"))!=NULL) {
			printf("Loading %s file ...\n", argv[1]);
			load_source_file(fsource, region, &traces, (void*(*)(int, FILE*, struct Region *, void *, time_t *, time_t *, struct Box *))load_trace_with_hashtable, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, &routes, (void*(*)(FILE*, struct Region*, void *))load_route_with_hashtable, NULL, NULL, NULL);
			fclose(fsource);
		}

		argc--;
		argv++;
	}


	if(traces.count != 0) {
	      for (i = 0; i<traces.size; i++) {
		      aItem = traces.head[i];
		      while(aItem != NULL ) {
				aTrace = (struct Trace*)aItem->datap;
				aRegion = NULL;
				bTrace = NULL;

				if(aTrace->type == FILE_MODIFIED_GPS_BUS && routes.count) {
					duallist_init(&roads);
					sprintf(buf, "%s_upway", aTrace->onRoute);
  					tItem = hashtable_find(&routes, buf);
					if(tItem) {
						aRoute = (struct Busroute*)tItem->datap;
						bItem = aRoute->path->roads.head;
						while(bItem) {
							duallist_add_to_tail(&roads, bItem->datap);
							bItem = bItem->next;
						}	
					}
					sprintf(buf, "%s_downway", aTrace->onRoute);
  					tItem = hashtable_find(&routes, buf);
					if(tItem) {
						aRoute = (struct Busroute*)tItem->datap;
						bItem = aRoute->path->roads.head;
						while(bItem) {
							duallist_add_to_tail(&roads, bItem->datap);
							bItem = bItem->next;
						}	
					}

					aRegion = build_region_with_roads(&roads);
					if(aRegion) {
		 				bTrace = insert_reports(aRegion, aTrace, insertMode, outputMode, interval);
						region_free_func(aRegion);
					}
					duallist_destroy(&roads, NULL);

				} else { 
		 			bTrace = insert_reports(region, aTrace, insertMode, outputMode, interval);
				}

				if(bTrace) {
					mkdir(directory,0777);
					sprintf(dumpfile, "%s/%s.mgd", directory, bTrace->vName);
					if( (fdump = fopen(dumpfile, "w"))!=NULL) {
						trace_dump_func(fdump, bTrace);
						fflush(fdump);
						fclose(fdump);
					}
					trace_free_func(bTrace);
				}
				trace_free_func((struct Trace*)hashtable_pick(&traces, aTrace->vName));

			      	aItem = aItem->next;
		      }
	      }
	} 
	 
	hashtable_destroy(&traces, (void(*)(void*))trace_free_func);
	hashtable_destroy(&routes, (void(*)(void*))route_free_func);
	region_free_func(region);
	return 0;
}
示例#7
0
int main(int argc, char **argv)
{
	FILE *fsource, *fdump;
	char dumpfile[256], *directory;

	struct Hashtable traces;
	long traceTableSize = 2000;
	struct Item *aItem, *bItem;
	struct Trace *aTrace;
	struct Report *aRep;

	unsigned long i;


	if(argc < 2) {
	      printf("%s is used to convert old version ogd to latest version.\n", argv[0]);
	      printf("Usage: %s [-d directory] (.ogd|.lst ...)\n", argv[0]);
	      exit(1);
	}

	directory = ".";
	while(argv[1][0] == '-') {
	      switch(argv[1][1]) {
	      case 'd':
		      directory = argv[2];
		      argc-=2;
		      argv+=2;
		      break;
	      default:
		      printf("Usage: %s [-d directory] (.ogd|.lst ...)\n", argv[0]);
	      }
	}

	hashtable_init(&traces, traceTableSize, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))trace_has_name);
	while(argc > 1) {
		if((fsource=fopen(argv[1], "r"))!=NULL) {
			printf("Loading %s file ...\n", argv[1]);
			load_source_file(fsource, NULL, &traces, (void*(*)(int, FILE*, struct Region *, struct Hashtable *, time_t *, time_t *, struct Box *))load_trace_with_hashtable, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
			fclose(fsource);
		}

		argc--;
		argv++;
	}

	printf("Altering the heading in old version ogd files & dumping...\n");
	for (i = 0; i<traces.size; i++) {
		aItem = traces.head[i];
		while(aItem != NULL ) {
			  aTrace = (struct Trace*)aItem->datap;
			  bItem = aTrace->reports.head;
			  while(bItem!=NULL) {
				  aRep = (struct Report*)bItem->datap;
				  aRep->angle = normal_angle_taxi(aRep->angle);
				  bItem = bItem->next;
			  }

			  sprintf(dumpfile, "%s/Taxi_%s.ogd", directory, aTrace->vName);
			  if( (fdump = fopen(dumpfile, "w"))!=NULL) {
				  trace_dump_func(fdump, aTrace);
				  fflush(fdump);
				  fclose(fdump);
			  }
			  aItem = aItem->next;
		}
	}
	 
	hashtable_destroy(&traces, (void(*)(void*))trace_free_func);
	return 0;
}