int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); std::vector<char*> argvs; argvs.push_back(strdup("--use_gpu=false")); paddle_init((int)argvs.size(), argvs.data()); for (auto each : argvs) { free(each); } return RUN_ALL_TESTS(); }
void set_up() { initscr(); /* turn on curses */ noecho(); /* turn off echo */ cbreak(); /* turn off buffering */ srand( getpid() ); /*seed the random num generator*/ signal(SIGINT, SIG_IGN); /* ignore SIGINT */ signal(SIGTERM, SIG_IGN); /* ignore SIGINT */ //Draw the court court_init( 3 ); //Init the scoreboard display ppcourt_geom court_g; court_geom( &court_g ); jmbtrn_init( court_g.top - 1, court_g.left + 1 ); /*Init's clock to 0*/ jmbtrn_display_ball_count( STARTING_BALL_COUNT ); //Init the referee referee_init(); //Init the paddle paddle_init( ); //Init and serve the ball ball_init( ); //Create a sigaction with "handle_tick" as the handler struct sigaction tick_handler; tick_handler.sa_handler = handle_tick; tick_handler.sa_flags |= SA_RESTART; /*Make sure to restart sys calls*/ if ( sigaction( SIGALRM, &tick_handler, NULL ) == -1 ){ perror( "Sigaction" ); exit( 1 ); }else{ if ( set_ticker( 1000 / (TICK_PER_SEC) ) == -1 ){/* send millisecs per tick */ perror( "Error setting ticker" ); exit( 1 ); } } }
int main() { // Initalize Paddle char* argv[] = {"--use_gpu=True"}; CHECK(paddle_init(1, (char**)argv)); // Reading config binary file. It is generated by `convert_protobin.sh` long size; void* buf = read_config(CONFIG_BIN, &size); // Create a gradient machine for inference. paddle_gradient_machine machine; CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size)); CHECK(paddle_gradient_machine_randomize_param(machine)); // Loading parameter. Uncomment the following line and change the directory. // CHECK(paddle_gradient_machine_load_parameter_from_disk(machine, // "./some_where_to_params")); srand(time(0)); pthread_mutex_init(&mutex, NULL); pthread_t threads[NUM_THREAD]; for (int i = 0; i < NUM_THREAD; ++i) { paddle_gradient_machine thread_local_machine; CHECK(paddle_gradient_machine_create_shared_param( machine, buf, size, &thread_local_machine)); pthread_create(&threads[i], NULL, thread_main, thread_local_machine); } for (int i = 0; i < NUM_THREAD; ++i) { pthread_join(threads[i], NULL); } pthread_mutex_destroy(&mutex); return 0; }
int main() { // Initalize Paddle char* argv[] = {"--use_gpu=False"}; CHECK(paddle_init(1, (char**)argv)); // Reading config binary file. It is generated by `convert_protobin.sh` long size; void* buf = read_config(CONFIG_BIN, &size); // Create a gradient machine for inference. paddle_gradient_machine machine; CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size)); CHECK(paddle_gradient_machine_randomize_param(machine)); // Loading parameter. Uncomment the following line and change the directory. // CHECK(paddle_gradient_machine_load_parameter_from_disk(machine, // "./some_where_to_params")); paddle_arguments in_args = paddle_arguments_create_none(); // There is only one input of this network. CHECK(paddle_arguments_resize(in_args, 1)); // Create input matrix. paddle_matrix mat = paddle_matrix_create(/* sample_num */ 1, /* size */ 784, /* useGPU */ false); srand(time(0)); paddle_real* array; // Get First row. CHECK(paddle_matrix_get_row(mat, 0, &array)); for (int i = 0; i < 784; ++i) { array[i] = rand() / ((float)RAND_MAX); } CHECK(paddle_arguments_set_value(in_args, 0, mat)); paddle_arguments out_args = paddle_arguments_create_none(); CHECK(paddle_gradient_machine_forward(machine, in_args, out_args, /* isTrain */ false)); paddle_matrix prob = paddle_matrix_create_none(); CHECK(paddle_arguments_get_value(out_args, 0, prob)); uint64_t height; uint64_t width; CHECK(paddle_matrix_get_shape(prob, &height, &width)); CHECK(paddle_matrix_get_row(prob, 0, &array)); printf("Prob: \n"); for (int i = 0; i < height * width; ++i) { printf("%.4f ", array[i]); if ((i + 1) % width == 0) { printf("\n"); } } printf("\n"); CHECK(paddle_matrix_destroy(prob)); CHECK(paddle_arguments_destroy(out_args)); CHECK(paddle_matrix_destroy(mat)); CHECK(paddle_arguments_destroy(in_args)); CHECK(paddle_gradient_machine_destroy(machine)); return 0; }
void pong_main(void) { Pong game; SDL_Window *window = NULL; //initialize SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "SDL not initialized: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } //create the window window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); if (!window) { fprintf(stderr, "SDL could not create window: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } //create a hardware accelerated renderer for the window game.renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE); //create the font texture SDL_RWops *s = SDL_RWFromMem(assets_font, assets_font_size); if (!s) { fprintf(stderr, "Unable to load font data: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } SDL_Surface *font = SDL_LoadBMP_RW(s, 1); //we close the stream automatically game.font = SDL_CreateTextureFromSurface(game.renderer, font); if (!game.font) { fprintf(stderr, "Unable to create font texture: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } SDL_FreeSurface(font); //create the sprites texture s = SDL_RWFromMem(assets_sprites, assets_sprites_size); if (!s) { fprintf(stderr, "Unable to load sprite data: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } SDL_Surface *sprites = SDL_LoadBMP_RW(s, 1); //we close the stream automatically game.sprites = SDL_CreateTextureFromSurface(game.renderer, sprites); if (!game.sprites) { fprintf(stderr, "Unable to create sprites texture: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } SDL_FreeSurface(sprites); //initial game settings game.player1Score = 0; game.player2Score = 0; game.ball.x = WINDOW_WIDTH / 2; game.ball.y = WINDOW_HEIGHT / 2; game.ballDir = BALL_DEFAULT_DIR; paddle_init(&game.player1, &game.player2); //main game loop while(1) { if (!handle_events()) { break; } //perform the game logic pong_tick(&game); //render the game pong_render(&game); } //destroy our window and associated resources SDL_DestroyTexture(game.font); SDL_DestroyRenderer(game.renderer); SDL_DestroyWindow(window); //all done! SDL_Quit(); exit(EXIT_SUCCESS); }
int main() { // Initalize Paddle char* argv[] = {"--use_gpu=False"}; CHECK(paddle_init(1, (char**)argv)); // Read the binary configuration file which is generated by // `convert_protobin.sh` long size; void* buf = read_config(CONFIG_BIN, &size); // Create the gradient machine for inference. paddle_gradient_machine machine; CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size)); CHECK(paddle_gradient_machine_randomize_param(machine)); // Load the trained parameters. Uncomment the following line and change the // directory as needed. // CHECK(paddle_gradient_machine_load_parameter_from_disk(machine, // "./some_where_to_params")); paddle_arguments in_args = paddle_arguments_create_none(); // There is only one input of this network. CHECK(paddle_arguments_resize(in_args, 1)); // Create the input matrix. paddle_matrix mat = paddle_matrix_create_sparse(1, 784, 3, true, false); srand(time(0)); paddle_real* array; int colBuf[] = {9, 93, 109}; int rowBuf[] = {0, sizeof(colBuf) / sizeof(int)}; CHECK(paddle_matrix_sparse_copy_from(mat, rowBuf, sizeof(rowBuf) / sizeof(int), colBuf, sizeof(colBuf) / sizeof(int), NULL, 0)); CHECK(paddle_arguments_set_value(in_args, 0, mat)); paddle_arguments out_args = paddle_arguments_create_none(); CHECK(paddle_gradient_machine_forward(machine, in_args, out_args, /* isTrain */ false)); paddle_matrix prob = paddle_matrix_create_none(); CHECK(paddle_arguments_get_value(out_args, 0, prob)); CHECK(paddle_matrix_get_row(prob, 0, &array)); printf("Prob: "); for (int i = 0; i < 10; ++i) { printf("%.2f ", array[i]); } printf("\n"); CHECK(paddle_matrix_destroy(prob)); CHECK(paddle_arguments_destroy(out_args)); CHECK(paddle_matrix_destroy(mat)); CHECK(paddle_arguments_destroy(in_args)); CHECK(paddle_gradient_machine_destroy(machine)); return 0; }