ソースコードの心得を読む


このようなコードがあって、ホストとスレーブの上ですべて実行することができて、コードは条件でコンパイルする方式で、読むのがとても気持ち悪くて、まるで考えを乱して、それから私はコードを開けて、やはり読みやすくなりました.
void
test_host_to_tile(int fd, void* buffer)
{
  PRINTF("Sending host-to-TLR :
"); #ifdef __tile__ printf("Tile PCIE driver will receive YUV files from host .....
"); while(1) { for (int j = 0; j < (FRAME_RATE_YUV *2); j++) { do_read(fd, yuv_rcv[j], FRAME_SIZE_YUV); //j %= (FRAME_RATE_YUV *2); //Bruce: we should add some sync mechanism for Encoding Tiles, make Encoding Tile to take the data to encode. //should adopt Medialib methods //TODO: #if 0 int* data_temp=(int *)yuv_rcv[j]; printf("received data is : "); for(int x=0;x <20; x++) { printf(" 0x%x ",*data_temp++); } #endif } } #else typedef struct Frame { int width; int height; int linesize[3]; }pcidata_t; //while(1) //{ pcidata_t pcidata; memset(&pcidata, 0, sizeof(pcidata_t)); pcidata.width = 1280; pcidata.height = 720; pcidata.linesize[0] = 1280; pcidata.linesize[1] = 640; pcidata.linesize[2] = 640; for (int i = 0; i < SHOW_TIME; i++) { #ifdef PRINTING struct timeval before, after; gettimeofday(&before, NULL); #endif for (int j = 0; j < FRAME_RATE_YUV; j++) { // do_write(fd, &pcidata, sizeof(pcidata_t)); printf("sending #%d (%d bytes@%p)...", j, FRAME_SIZE_YUV, yuv_data[i][j]); do_write(fd, yuv_data[i][j], FRAME_SIZE_YUV); printf("done
"); //delay 30 ms, usleep(1000*30); } //i %= SHOW_TIME; printf("send 25 frames
"); #ifdef PRINTING gettimeofday(&after, NULL); double secs = after.tv_sec - before.tv_sec; double usecs = after.tv_usec - before.tv_usec; double time = secs + usecs * 1e-6; PRINTF("%f seconds for %d bytes, %f Mb/s
", time, TEST_SIZE, (TEST_SIZE * 8) / time / 1e6); #endif } //} #endif } void test_tile_to_host(int fd, void* buffer) { PRINTF("bandwidth TLR-to-host test:
"); for (int i = 0; i < ITERATIONS; i++) { #ifdef PRINTING struct timeval before, after; gettimeofday(&before, NULL); #endif for (int j = 0; j < (FRAME_RATE_YUV *4); j++) { #ifdef __tile__ do_write(fd, buffer, BUFFER_SIZE); #else do_read(fd, yuv_host[j], FRAME_SIZE_YUV); #endif } #ifdef PRINTING gettimeofday(&after, NULL); double secs = after.tv_sec - before.tv_sec; double usecs = after.tv_usec - before.tv_usec; double time = secs + usecs * 1e-6; PRINTF("%f seconds for %d bytes, %f Mb/s
", time, TEST_SIZE, (TEST_SIZE * 8) / time / 1e6); #endif } }
int
main()
{
    int fd = open_channel(2);
#ifdef __tile__
    //added by Bruce, malloc buffers to store all the frames in 4 seconds
    int j;
    for (j=0; j< (FRAME_RATE_YUV*2); j++)
        {
         yuv_rcv[j] = malloc(FRAME_SIZE_YUV);
          if (yuv_rcv[j] == NULL)
          {
            fprintf(stderr, "Failed to allocate buffer.
"); return 1; } memset(yuv_rcv[j], 0, FRAME_SIZE_YUV); } printf("Tilera chip allocate all the buffer Ok.
"); //Bruce: for Tile, we should create other Encoding threads before reading YUV data into buffers //TODO: #else int i,j; int header =0; char * yuv_filename = "./outyuvfile.yuv"; //char * yuv_filename = "/opt/same_JM_1080p25_4Mbps.yuv"; //char * output_name = "out.264"; printf("began to allocate buffers
"); //added by Bruce, malloc buffers to store all the frames in 4 seconds for (i=0; i< SHOW_TIME; i++) { for (j=0; j< FRAME_RATE_YUV; j++) { yuv_data[i][j] = malloc(FRAME_SIZE_YUV); if (yuv_data[i][j] == NULL) { fprintf(stderr, "Failed to allocate buffer.
"); return 1; } memset(yuv_data[i][j], 0x0, FRAME_SIZE_YUV); } } printf("X86 Driver allocate all the buffers OK
"); //read YUV file into buffers FILE *f=fopen(yuv_filename, "rb"); fseek(f, header,SEEK_SET); for (i=0; i< SHOW_TIME; i++) { for (j=0; j< FRAME_RATE_YUV; j++) { int ret= fread(yuv_data[i][j], 1,FRAME_SIZE_YUV,f); if (ret != FRAME_SIZE_YUV) { fprintf(stderr, "Failed to read frames i %d: j %d .
",i,j); return 1; } } } fclose(f); for (j=0; j< (FRAME_RATE_YUV*2); j++) { yuv_host[j] = malloc(FRAME_SIZE_YUV); if (yuv_host[j] == NULL) { fprintf(stderr, "Failed to allocate buffer.
"); return 1; } memset(yuv_host[j], 0, FRAME_SIZE_YUV); } printf("Host chip allocate all the buffer Ok.
"); #endif #ifdef __tile__ test_host_to_tile(fd,yuv_rcv); #else test_host_to_tile(fd, yuv_data); #endif // test_tile_to_host(fd, yuv_data); PRINTF("Bandwidth test complete.

"); close(fd); return 0; }

分離後、マシンから得られたコードは:
void
test_host_to_tile(int fd, void* buffer)
{
  PRINTF("Sending host-to-TLR :
"); printf("Tile PCIE driver will receive YUV files from host .....
"); while(1) { for (int j = 0; j < (FRAME_RATE_YUV *2); j++) { do_read(fd, yuv_rcv[j], FRAME_SIZE_YUV); //j %= (FRAME_RATE_YUV *2); //Bruce: we should add some sync mechanism for Encoding Tiles, make Encoding Tile to take the data to encode. //should adopt Medialib methods //TODO: } } }

スレーブtest_tile_to_host関数は次のとおりです.
void
test_tile_to_host(int fd, void* buffer)
{
  PRINTF("bandwidth TLR-to-host test:
"); for (int i = 0; i < ITERATIONS; i++) { for (int j = 0; j < (FRAME_RATE_YUV *4); j++) { do_write(fd, buffer, BUFFER_SIZE); } } }

スレーブmain関数は次のとおりです.
int
main()
{
    int fd = open_channel(2);
    //added by Bruce, malloc buffers to store all the frames in 4 seconds
    int j;
    for (j=0; j< (FRAME_RATE_YUV*2); j++)
        {
         yuv_rcv[j] = malloc(FRAME_SIZE_YUV);
          if (yuv_rcv[j] == NULL)
          {
            fprintf(stderr, "Failed to allocate buffer.
"); return 1; } memset(yuv_rcv[j], 0, FRAME_SIZE_YUV); } printf("Tilera chip allocate all the buffer Ok.
"); //Bruce: for Tile, we should create other Encoding threads before reading YUV data into buffers //TODO: test_host_to_tile(fd,yuv_rcv); // test_tile_to_host(fd, yuv_data); PRINTF("Bandwidth test complete.

"); close(fd); return 0; }

ホストでは、次の3つの関数があります.
host_to_tile
void
test_host_to_tile(int fd, void* buffer)
{
  PRINTF("Sending host-to-TLR :
"); typedef struct Frame { int width; int height; int linesize[3]; }pcidata_t; //while(1) //{ pcidata_t pcidata; memset(&pcidata, 0, sizeof(pcidata_t)); pcidata.width = 1280; pcidata.height = 720; pcidata.linesize[0] = 1280; pcidata.linesize[1] = 640; pcidata.linesize[2] = 640; for (int i = 0; i < SHOW_TIME; i++) { struct timeval before, after; gettimeofday(&before, NULL); for (int j = 0; j < FRAME_RATE_YUV; j++) { // do_write(fd, &pcidata, sizeof(pcidata_t)); printf("sending #%d (%d bytes@%p)...", j, FRAME_SIZE_YUV, yuv_data[i][j]); do_write(fd, yuv_data[i][j], FRAME_SIZE_YUV); printf("done
"); //delay 30 ms, usleep(1000*30); } //i %= SHOW_TIME; printf("send 25 frames
"); gettimeofday(&after, NULL); double secs = after.tv_sec - before.tv_sec; double usecs = after.tv_usec - before.tv_usec; double time = secs + usecs * 1e-6; PRINTF("%f seconds for %d bytes, %f Mb/s
", time, TEST_SIZE, (TEST_SIZE * 8) / time / 1e6); } //} }

そしてtile_to_hostコードは
void
test_tile_to_host(int fd, void* buffer)
{
  PRINTF("bandwidth TLR-to-host test:
"); for (int i = 0; i < ITERATIONS; i++) { struct timeval before, after; gettimeofday(&before, NULL); for (int j = 0; j < (FRAME_RATE_YUV *4); j++) { do_read(fd, yuv_host[j], FRAME_SIZE_YUV); } gettimeofday(&after, NULL); double secs = after.tv_sec - before.tv_sec; double usecs = after.tv_usec - before.tv_usec; double time = secs + usecs * 1e-6; PRINTF("%f seconds for %d bytes, %f Mb/s
", time, TEST_SIZE, (TEST_SIZE * 8) / time / 1e6); } }

main関数は次のとおりです.
int
main()
{
    int fd = open_channel(2);
    int i,j;
    int header =0;
    char * yuv_filename = "./outyuvfile.yuv";
    //char * yuv_filename = "/opt/same_JM_1080p25_4Mbps.yuv";
    //char * output_name = "out.264";
    printf("began to allocate buffers 
"); //added by Bruce, malloc buffers to store all the frames in 4 seconds for (i=0; i< SHOW_TIME; i++) { for (j=0; j< FRAME_RATE_YUV; j++) { yuv_data[i][j] = malloc(FRAME_SIZE_YUV); if (yuv_data[i][j] == NULL) { fprintf(stderr, "Failed to allocate buffer.
"); return 1; } memset(yuv_data[i][j], 0x0, FRAME_SIZE_YUV); } } printf("X86 Driver allocate all the buffers OK
"); //read YUV file into buffers FILE *f=fopen(yuv_filename, "rb"); fseek(f, header,SEEK_SET); for (i=0; i< SHOW_TIME; i++) { for (j=0; j< FRAME_RATE_YUV; j++) { int ret= fread(yuv_data[i][j], 1,FRAME_SIZE_YUV,f); if (ret != FRAME_SIZE_YUV) { fprintf(stderr, "Failed to read frames i %d: j %d .
",i,j); return 1; } } } fclose(f); for (j=0; j< (FRAME_RATE_YUV*2); j++) { yuv_host[j] = malloc(FRAME_SIZE_YUV); if (yuv_host[j] == NULL) { fprintf(stderr, "Failed to allocate buffer.
"); return 1; } memset(yuv_host[j], 0, FRAME_SIZE_YUV); } printf("Host chip allocate all the buffer Ok.
"); #endif test_host_to_tile(fd, yuv_data); // test_tile_to_host(fd, yuv_data); PRINTF("Bandwidth test complete.

"); close(fd); return 0; }