Advertisement
Guest User

Untitled

a guest
May 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cassert>
  4. #include <math.h>
  5. #include "pipeline_native.h"
  6. #include "pipeline_zynq.h"
  7.  
  8. #include "halide_image_io.h"
  9. #include "HalideBuffer.h"
  10.  
  11. using namespace Halide::Tools;
  12. using namespace Halide::Runtime;
  13.  
  14. extern "C" {
  15. #ifndef CMA_BUFFER_T_DEFINED
  16. #define CMA_BUFFER_T_DEFINED
  17. struct mMap;
  18. typedef struct cma_buffer_t {
  19.   unsigned int id; // ID flag for internal use
  20.   unsigned int width; // Width of the image
  21.   unsigned int stride; // Stride between rows, in pixels. This must be >= width
  22.   unsigned int height; // Height of the image
  23.   unsigned int depth; // Byte-depth of the image
  24.   unsigned int phys_addr; // Bus address for DMA
  25.   void* kern_addr; // Kernel virtual address
  26.   struct mMap* cvals;
  27.   unsigned int mmap_offset;
  28. } cma_buffer_t;
  29. #endif
  30. // Zynq runtime API
  31. int halide_zynq_init();
  32. void halide_zynq_free(void *user_context, void *ptr);
  33. int halide_zynq_cma_alloc(struct buffer_t *buf);
  34. int halide_zynq_cma_free(struct buffer_t *buf);
  35. int halide_zynq_subimage(const struct buffer_t* image, struct cma_buffer_t* subimage, void *address_of_subimage_origin, int width, int height);
  36. int halide_zynq_hwacc_launch(struct cma_buffer_t bufs[]);
  37. int halide_zynq_hwacc_sync(int task_id);
  38. }
  39.  
  40. int main(int argc, char **argv) {
  41.     if (argc != 2) {
  42.         fprintf(stderr, "Usage: %s <img_path>\n", argv[0]);
  43.         return 1;
  44.     }
  45.     halide_zynq_init();
  46.  
  47.     Buffer<uint8_t> input = load_image(argv[1]);
  48.     fprintf(stderr, "%d %d\n", input.width(), input.height());
  49.     Buffer<uint8_t> out_native(720, 480, 3);
  50.     Buffer<uint8_t> out_zynq_img(720, 480, 3);
  51.  
  52.     // prepare a pinned buffer holding inputs
  53.     buffer_t input_zynq = {0};
  54.     input_zynq.elem_size = 1;
  55.     input_zynq.extent[0] = 1920;
  56.     input_zynq.extent[1] = 1080;
  57.     input_zynq.stride[0] = 1;
  58.     input_zynq.stride[1] = 1920;
  59.  
  60.     // prepare a pinned buffer holding outputs
  61.     buffer_t output_zynq = {0};
  62.     output_zynq.elem_size = 1;
  63.     output_zynq.extent[0] = 3;
  64.     output_zynq.extent[1] = 720;
  65.     output_zynq.extent[2] = 480;
  66.     output_zynq.stride[0] = 1;
  67.     output_zynq.stride[1] = 3;
  68.     output_zynq.stride[2] = 2160;
  69.    
  70.     halide_zynq_cma_alloc(&input_zynq);
  71.    
  72.     // fill data
  73.     for (int y = 0; y < input_zynq.extent[1]; y++)
  74.         for (int x = 0; x < input_zynq.extent[0]; x++)
  75.             input_zynq.host[x + y*input_zynq.stride[1]] = input(x, y);
  76.  
  77.     halide_zynq_cma_alloc(&output_zynq);
  78.    
  79.     printf("start.\n");
  80.     pipeline_native(input, out_native);
  81.     save_image(out_native, "out_native.png");
  82.     printf("cpu program results saved.\n");
  83.     //out_native = load_image("out_native.png");
  84.     //printf("cpu program results loaded.\n");
  85.  
  86.    
  87.     pipeline_zynq(&input_zynq, &output_zynq);
  88.  
  89.     // copy output values
  90.     for (int y = 0; y < output_zynq.extent[2]; y++)
  91.         for (int x = 0; x < output_zynq.extent[1]; x++)
  92.             for (int c = 0; c < output_zynq.extent[0]; c++)
  93.                 out_zynq_img(x, y, c) = output_zynq.host[c + x*output_zynq.stride[1] +
  94.                                                          y*output_zynq.stride[2]];
  95.  
  96.     save_image(out_zynq_img, "out_zynq_bypass.png");
  97.     printf("accelerator program results saved.\n");
  98.  
  99.     printf("checking results...\n");
  100.  
  101.     unsigned fails = 0;
  102.     for (int y = 0; y < out_zynq_img.height(); y++) {
  103.         for (int x = 0; x < out_zynq_img.width(); x++) {
  104.             for (int c = 0; c < 3; c++) {
  105.                 if (out_native(x, y, c) != out_zynq_img(x, y, c)) {
  106.                     //printf("out_native(%d, %d, %d) = %d, but out_zynq_img(%d, %d, %d) = %d\n",
  107.                     //       x, y, c, out_native(x, y, c),
  108.                     //       x, y, c, out_zynq_img(x, y, c));
  109.                     fails++;
  110.                     goto failed;
  111.                 }
  112.             }
  113.         }
  114.     }
  115. failed:
  116.     if (!fails) {
  117.         printf("passed.\n");
  118.     } else  {
  119.         printf("%u fails.\n", fails);
  120.     }
  121.  
  122.  
  123.     // free pinned buffers
  124.     halide_zynq_cma_free(&input_zynq);
  125.     halide_zynq_cma_free(&output_zynq);
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement