File: EGLSync_CUDAEvent_Interop.cu

package info (click to toggle)
nvidia-cuda-samples 12.4.1~dfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 313,216 kB
  • sloc: cpp: 82,042; makefile: 53,971; xml: 15,381; ansic: 8,630; sh: 91; python: 74
file content (783 lines) | stat: -rw-r--r-- 25,369 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of NVIDIA CORPORATION nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// Simple interop app demonstrating EGLImage + EGLSync interop with CUDA.
// Using EGLSync - CUDA Event interop one can achieve synchronization on GPU
// itself for GL-EGL-CUDA operations instead of blocking CPU for
// synchronization. This app requires GLES 3.2 or higher

//---------------------------INCLUDES---------------------------------//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "graphics_interface.h"
#include <cuda.h>
#include <helper_cuda_drvapi.h>
#include <cudaEGL.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl32.h>
#include "egl_common.h"

//---------------------------DEFINES---------------------------------//
#define MAX_ITR 100

#define FAILURE 0
#define SUCCESS 1
#define WAIVED 2

#define BLOCK_SIZE 16

#define GL_READ 0
#define GL_WRITE 1
//---------------------------MACROS---------------------------------//

// Error-checking wrapper around GL calls
#define GL_SAFE_CALL(call)                                              \
  {                                                                     \
    GLenum err;                                                         \
    call;                                                               \
    err = glGetError();                                                 \
    if (err != GL_NO_ERROR) {                                           \
      fprintf(stderr, "%s:%d GL error: %d\n", __FILE__, __LINE__, err); \
      cleanup(FAILURE);                                                 \
    }                                                                   \
  }

#define GL_SAFE_CALL_NO_CLEANUP(call, err)                                 \
  {                                                                        \
    GLenum status;                                                         \
    call;                                                                  \
    status = glGetError();                                                 \
    if (status != GL_NO_ERROR) {                                           \
      fprintf(stderr, "%s:%d GL error: %d\n", __FILE__, __LINE__, status); \
      err = status;                                                        \
    }                                                                      \
  }

// Error-checking wrapper around CUDA calls (taken from cutil.h)
#define CUDA_SAFE_CALL(call)                                                  \
  do {                                                                        \
    cudaError err = call;                                                     \
    if (cudaSuccess != err) {                                                 \
      fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", __FILE__, \
              __LINE__, cudaGetErrorString(err));                             \
      cleanup(FAILURE);                                                       \
    }                                                                         \
  } while (0)

#define CUDA_SAFE_CALL_NO_CLEANUP(call, err)                                  \
  do {                                                                        \
    cudaError status = call;                                                  \
    if (cudaSuccess != status) {                                              \
      fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", __FILE__, \
              __LINE__, cudaGetErrorString(status));                          \
      err = status;                                                           \
    }                                                                         \
  } while (0)

#if defined(EXTENSION_LIST)
EXTENSION_LIST(EXTLST_DECL)
typedef void (*extlst_fnptr_t)(void);
static struct {
  extlst_fnptr_t *fnptr;
  char const *name;
} extensionList[] = {EXTENSION_LIST(EXTLST_ENTRY)};

int eglSetupExtensions(void) {
  unsigned int i;

  for (i = 0; i < (sizeof(extensionList) / sizeof(*extensionList)); i++) {
    *extensionList[i].fnptr = eglGetProcAddress(extensionList[i].name);
    if (*extensionList[i].fnptr == NULL) {
      printf("Couldn't get address of %s()\n", extensionList[i].name);
      return 0;
    }
  }

  return 1;
}
#endif

#if defined(EXTENSION_LIST)
EXTENSION_LIST(EXTLST_EXTERN)
#endif

//------------------------GLOBAL VARIABLES--------------------------//

// GL texture
GLuint tex[2] = {0};

// Used to catch unexpected termination from GLUT
int cleanExit = 0;

// Use CPU Sync or GPU sync; Default GPU
int useGpu = 1;

// CUDA Resource
CUgraphicsResource writeResource = NULL;
CUgraphicsResource readResource = NULL;
CUarray writeArray, readArray;
CUdevice device;
CUcontext context;

// Which device to run on
unsigned int dev = 0;

// Default width, height, and iterations value
int width = 2048;
int height = 2048;
int itr = MAX_ITR;

// Error check variable
__device__ static unsigned int numErrors = 0;

//-----------------------FUNCTION PROTOTYPES------------------------//

void checkSync(int argc, char **argv);
int parseCmdLine(int argc, char **argv);
void printUsage(void);
void cleanup(int status);
void exitHandler(void);
void printStatus(int status);
void checkSyncOnCPU(void);
void checkSyncOnGPU(EGLDisplay dpy);

__global__ void verify_and_update_kernel(CUsurfObject write, CUsurfObject read,
                                         char expected, char newval, int width,
                                         int height);
extern "C" cudaError_t cudaGetValueMismatch();

//-----------------------FUNCTION DEFINITIONS------------------------//

int main(int argc, char *argv[]) {
#if defined(__linux__)
  setenv("DISPLAY", ":0", 0);
#endif

  parseCmdLine(argc, argv);
  atexit(exitHandler);

  checkSync(argc, argv);
  return 0;
}

int parseCmdLine(int argc, char **argv) {
  int i;
  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-cpu") == 0) {
      useGpu = 0;
    }

    if (strcmp(argv[i], "-h") == 0) {
      printUsage();
      cleanup(SUCCESS);
    }

    if (strcmp(argv[i], "-width") == 0) {
      ++i;
      if (i == argc) {
        printf("width option must be followed by value\n");
        return FAILURE;
      }
      if (sscanf(argv[i], "%d", &width) != 1) {
        printf("Error: invalid width value\n");
        return FAILURE;
      }
    }

    if (strcmp(argv[i], "-height") == 0) {
      ++i;
      if (i == argc) {
        printf("height option must be followed by value\n");
        return FAILURE;
      }
      if (sscanf(argv[i], "%d", &height) != 1) {
        printf("Error: invalid height value\n");
        return FAILURE;
      }
    }
    if (strcmp(argv[i], "-itr") == 0) {
      ++i;
      if (i == argc) {
        printf("itr option must be followed by iteration value\n");
        return FAILURE;
      }
      if (sscanf(argv[i], "%d", &itr) != 1) {
        printf("Error: invalid iteration value\n");
        return FAILURE;
      }
    }
  }

  return SUCCESS;
}

void printUsage(void) {
  printf("Usage:\n");
  printf("\t-h\tPrint command line options\n");
  printf("\t-cpu\tSync on the CPU instead of the GPU\n");
  printf("\t-width w\tSet the width to w\n");
  printf("\t-height h\tSet the height to h\n");
  printf("\t-itr i\tSet number of iterations to i\n");
}

void checkSync(int argc, char **argv) {
  int x, y;
  int bufferSize = width * height * 4;
  unsigned char *pSurf_read = NULL, *pSurf_write = NULL;
  int integrated;

  CUresult status = CUDA_SUCCESS;

  // Init values for variables
  x = y = 0;

  if (CUDA_SUCCESS != (status = cuInit(0))) {
    printf("Failed to initialize CUDA\n");
  }
  device = findCudaDeviceDRV(argc, (const char **)argv);

  if (CUDA_SUCCESS != (status = cuCtxCreate(&context, 0, device))) {
    printf("failed to create CUDA context\n");
  }
  cuCtxPushCurrent(context);

  status =
      cuDeviceGetAttribute(&integrated, CU_DEVICE_ATTRIBUTE_INTEGRATED, device);
  if (status != CUDA_SUCCESS) {
    printf("Failed to get device attribute CU_DEVICE_ATTRIBUTE_INTEGRATED\n");
    cleanup(FAILURE);
  }

  if (integrated != 1) {
    printf(
        "EGLSync_CUDAEvent_Interop does not support dGPU. Waiving sample.\n");
    cleanup(WAIVED);
  }

#if (defined(__arm__) || defined(__aarch64__)) && defined(__linux__)
  graphics_setup_window(0, 0, width, height, "EGLSync_CUDA_Interop");
#endif

  pSurf_read = (unsigned char *)malloc(bufferSize);
  pSurf_write = (unsigned char *)malloc(bufferSize);
  if (pSurf_read == NULL || pSurf_write == NULL) {
    printf("malloc failed\n");
    cleanup(FAILURE);
  }

  for (x = 0; x < width; x++) {
    for (y = 0; y < height; y++) {
      pSurf_read[(y * width + x) * 4] = 1;
      pSurf_read[(y * width + x) * 4 + 1] = 1;
      pSurf_read[(y * width + x) * 4 + 2] = 1;
      pSurf_read[(y * width + x) * 4 + 3] = 1;
      pSurf_write[(y * width + x) * 4] = 0;
      pSurf_write[(y * width + x) * 4 + 1] = 0;
      pSurf_write[(y * width + x) * 4 + 2] = 0;
      pSurf_write[(y * width + x) * 4 + 3] = 0;
    }
  }

  // NOP call to error-check the above glut calls
  GL_SAFE_CALL({});

  // Init texture
  GL_SAFE_CALL(glGenTextures(2, tex));

  GL_SAFE_CALL(glBindTexture(GL_TEXTURE_2D, tex[GL_READ]));
  GL_SAFE_CALL(
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
  GL_SAFE_CALL(
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
  GL_SAFE_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
                            GL_RGBA, GL_UNSIGNED_BYTE, pSurf_read));
  GL_SAFE_CALL(glBindTexture(GL_TEXTURE_2D, 0));

  GL_SAFE_CALL(glBindTexture(GL_TEXTURE_2D, tex[GL_WRITE]));
  GL_SAFE_CALL(
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
  GL_SAFE_CALL(
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
  GL_SAFE_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
                            GL_RGBA, GL_UNSIGNED_BYTE, pSurf_write));
  GL_SAFE_CALL(glBindTexture(GL_TEXTURE_2D, 0));

  glFinish();

  EGLDisplay eglDisplayHandle = eglGetCurrentDisplay();
  if (eglDisplayHandle == EGL_NO_DISPLAY) {
    printf("eglDisplayHandle failed \n");
    cleanup(FAILURE);
  } else {
    printf("eglDisplay Handle created \n");
  }

  if (!eglSetupExtensions()) {
    printf("SetupExtentions failed \n");
    cleanup(FAILURE);
  }

  EGLContext eglCtx = eglGetCurrentContext();
  if (eglCtx == EGL_NO_CONTEXT) {
    printf("Context1 create failed with error %d\n", eglGetError());
    cleanup(FAILURE);
  }

  // Create the EGL_Image
  EGLint eglImgAttrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE,
                          EGL_NONE};

  EGLImageKHR eglImage1 =
      eglCreateImageKHR(eglDisplayHandle, eglCtx, EGL_GL_TEXTURE_2D_KHR,
                        (EGLClientBuffer)(intptr_t)tex[GL_READ], eglImgAttrs);
  if (eglImage1 == EGL_NO_IMAGE_KHR) {
    printf("EGLImage create failed for read texture with error %d\n",
           eglGetError());
    cleanup(FAILURE);
  } else {
    printf("EGLImage1 created \n");
  }

  EGLImageKHR eglImage2 =
      eglCreateImageKHR(eglDisplayHandle, eglCtx, EGL_GL_TEXTURE_2D_KHR,
                        (EGLClientBuffer)(intptr_t)tex[GL_WRITE], eglImgAttrs);
  if (eglImage2 == EGL_NO_IMAGE_KHR) {
    printf("EGLImage create failed for write texture with error %d\n",
           eglGetError());
    cleanup(FAILURE);
  } else {
    printf("EGLImage2 created \n");
  }

  glFinish();

  status = cuGraphicsEGLRegisterImage(&writeResource, eglImage1,
                                      CU_GRAPHICS_REGISTER_FLAGS_NONE);
  if (status != CUDA_SUCCESS) {
    printf("cuGraphicsEGLRegisterImage failed with Texture 1\n");
    cleanup(FAILURE);
  } else {
    printf(
        "cuGraphicsEGLRegisterImage Passed, writeResource created with texture "
        "1\n");
  }

  status =
      cuGraphicsSubResourceGetMappedArray(&writeArray, writeResource, 0, 0);
  if (status != CUDA_SUCCESS) {
    printf(
        "cuGraphicsSubResourceGetMappedArray failed for writeResource with "
        "texture 1\n");
    cleanup(FAILURE);
  }

  status = cuGraphicsEGLRegisterImage(&readResource, eglImage2,
                                      CU_GRAPHICS_REGISTER_FLAGS_NONE);
  if (status != CUDA_SUCCESS) {
    printf(
        "cuGraphicsEGLRegisterImage failed for readResource with Texture 2\n");
    cleanup(FAILURE);
  } else {
    printf(
        "cuGraphicsEGLRegisterImage Passed, readResource created with texture "
        "2\n");
  }

  status = cuGraphicsSubResourceGetMappedArray(&readArray, readResource, 0, 0);
  if (status != CUDA_SUCCESS) {
    printf("cuGraphicsSubResourceGetMappedArray failed for texture 2\n");
    cleanup(FAILURE);
  }

  if (useGpu) {
    printf("Using GPU Sync path\n");
    checkSyncOnGPU(eglDisplayHandle);
  } else {
    printf("Using CPU Sync path\n");
    checkSyncOnCPU();
  }

  free(pSurf_read);
  free(pSurf_write);
  cleanup(SUCCESS);
}

void checkSyncOnCPU(void) {
  int z = 0;
  unsigned char expectedData, newData;
  CUresult status = CUDA_SUCCESS;
  CUDA_RESOURCE_DESC wdsc, rdsc;
  memset(&wdsc, 0, sizeof(wdsc));
  memset(&rdsc, 0, sizeof(rdsc));

  expectedData = 0;
  newData = 1;

  wdsc.resType = CU_RESOURCE_TYPE_ARRAY;
  wdsc.res.array.hArray = writeArray;
  CUsurfObject writeSurface;
  rdsc.resType = CU_RESOURCE_TYPE_ARRAY;
  rdsc.res.array.hArray = readArray;
  CUsurfObject readSurface;

  status = cuSurfObjectCreate(&writeSurface, &wdsc);
  if (status != CUDA_SUCCESS) {
    printf("Surface bounding failed with status %d\n", status);
    cleanup(FAILURE);
  }
  status = cuSurfObjectCreate(&readSurface, &rdsc);
  if (status != CUDA_SUCCESS) {
    printf("Surface bounding failed\n");
    cleanup(FAILURE);
  }

  for (z = 0; z < itr; z++) {
    // GL call to copy from read texture to write texture
    GL_SAFE_CALL(glCopyImageSubData(tex[GL_READ], GL_TEXTURE_2D, 0, 0, 0, 0,
                                    tex[GL_WRITE], GL_TEXTURE_2D, 0, 0, 0, 0,
                                    width, height, 1));

    glFinish();

    newData++;
    expectedData++;

    verify_and_update_kernel<<<(width * height) / 256, 256>>>(
        writeSurface, readSurface, expectedData, newData, width, height);

    status = cuCtxSynchronize();
    if (status != CUDA_SUCCESS) {
      printf("cuCtxSynchronize failed \n");
    }
  }

  cudaError_t err = cudaGetValueMismatch();
  if (err != cudaSuccess) {
    printf("Value mismatch seen when using CPU sync\n");
    cleanup(FAILURE);
  }

  // Clean up CUDA writeResource
  status = cuGraphicsUnregisterResource(writeResource);
  if (status != CUDA_SUCCESS) {
    printf("Failed to unregister %d", status);
    cleanup(FAILURE);
  } else {
    printf("Unregistered writeResource. \n");
  }

  // Clean up CUDA readResource
  status = cuGraphicsUnregisterResource(readResource);
  if (status != CUDA_SUCCESS) {
    printf("Failed to unregister %d", status);
    cleanup(FAILURE);
  } else {
    printf("Unregistered readResource. \n");
  }
}

/*
    Performs same function as checkSyncOnCPU
    Here instead of glFinish() and cuCtxSynchronize like in checkSyncOnCPU,
    we make use of EGLSync, CUDA Event and cuStreamWaitEvent, eglWaitSyncKHR to
   achieve the synchronization due to this CPU is not blocked for any
   synchronization needed between GL-EGL & CUDA operations all synchronizations
   happens on the GPU only.
*/
void checkSyncOnGPU(EGLDisplay dpy) {
  int z = 0;
  unsigned char expectedData, newData;
  cudaError_t err;
  CUresult status = CUDA_SUCCESS;
  CUstream stream;
  CUevent timingDisabledEvent;
  CUDA_RESOURCE_DESC wdsc, rdsc;
  memset(&wdsc, 0, sizeof(wdsc));
  memset(&rdsc, 0, sizeof(rdsc));

  expectedData = 0;
  newData = 1;

  wdsc.resType = CU_RESOURCE_TYPE_ARRAY;
  wdsc.res.array.hArray = writeArray;
  CUsurfObject writeSurface;
  rdsc.resType = CU_RESOURCE_TYPE_ARRAY;
  rdsc.res.array.hArray = readArray;
  CUsurfObject readSurface;

  status = cuSurfObjectCreate(&writeSurface, &wdsc);
  if (status != CUDA_SUCCESS) {
    printf("Surface bounding failed with status %d\n", status);
    cleanup(FAILURE);
  }
  status = cuSurfObjectCreate(&readSurface, &rdsc);
  if (status != CUDA_SUCCESS) {
    printf("Surface bounding failed\n");
    cleanup(FAILURE);
  }

  status = cuStreamCreate(&stream, CU_STREAM_DEFAULT);
  if (status != CUDA_SUCCESS) {
    printf("Stream creation failed\n");
    cleanup(FAILURE);
  }

  // Creates timing disabled event which uses non-blocking synchronization
  status = cuEventCreate(&timingDisabledEvent, CU_EVENT_DISABLE_TIMING);
  if (status != CUDA_SUCCESS) {
    printf("Default event creation failed\n");
    cleanup(FAILURE);
  }

  /*
      1. We perform texture-to-texture copy in GLES which is async function
      2. Followed by creating EGLSync and a CUDA Event from that EGLSync object
      3. Using cuStreamWaitEvent() we wait in GPU for the GLES to finish texture
     copy.
      4. CUDA kernel verfiy_and_update_kernel verifies if the copied data by
     GLES is correct, and it updates the buffer with new values.
      5. This is followed by eglWaitSyncKHR() which waits for the cuda kernel to
     finish, so that in the next iteration GLES can perform the copying of the
     updated buffer to write texture,
  */
  for (z = 0; z < itr; z++) {
    // GL call to copy from read texture to write texture
    GL_SAFE_CALL(glCopyImageSubData(tex[GL_READ], GL_TEXTURE_2D, 0, 0, 0, 0,
                                    tex[GL_WRITE], GL_TEXTURE_2D, 0, 0, 0, 0,
                                    width, height, 1));

    EGLSyncKHR eglSyncForGL, eglSyncForCuda;
    EGLBoolean egl_status = EGL_TRUE;
    EGLAttribKHR eglattrib[] = {EGL_CUDA_EVENT_HANDLE_NV,
                                (EGLAttrib)timingDisabledEvent, EGL_NONE};

    CUevent cudaEGLSyncEvent;

    eglSyncForGL = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);

    if (eglSyncForGL == EGL_NO_SYNC_KHR) {
      printf(" EGL Sync creation failed\n");
      cleanup(FAILURE);
    }

    status = cuEventCreateFromEGLSync(&cudaEGLSyncEvent, eglSyncForGL,
                                      CU_EVENT_DEFAULT);
    if (status != CUDA_SUCCESS) {
      printf("CUDA event creation from EGLSync failed\n");
      cleanup(FAILURE);
    }

    // We wait from CUDA in GPU for GL-EGL operation completion
    status = cuStreamWaitEvent(stream, cudaEGLSyncEvent, 0);
    if (status != CUDA_SUCCESS) {
      printf("Stream wait for event created from EGLSync failed\n");
      cleanup(FAILURE);
    }

    egl_status = eglDestroySyncKHR(dpy, eglSyncForGL);
    if (egl_status != EGL_TRUE) {
      printf("EGL sync object destruction failed\n");
      cleanup(FAILURE);
    }

    newData++;
    expectedData++;

    // Verifies the values in readSurface which is copied by
    // glCopyImageSubData() And writes value of newData into writeSurface
    verify_and_update_kernel<<<(width * height) / 256, 256, 0, stream>>>(
        writeSurface, readSurface, expectedData, newData, width, height);

    status = cuEventDestroy(cudaEGLSyncEvent);
    if (status != CUDA_SUCCESS) {
      printf("Event Destroy failed\n");
      cleanup(FAILURE);
    }

    status = cuEventRecord(timingDisabledEvent, stream);
    if (status != CUDA_SUCCESS) {
      printf("Event Record failed\n");
      cleanup(FAILURE);
    }

    // creating an EGL sync object linked to a CUDA event object
    eglSyncForCuda = eglCreateSync64KHR(dpy, EGL_SYNC_CUDA_EVENT_NV, eglattrib);

    // We wait from EGL for CUDA operation completion
    egl_status = eglWaitSyncKHR(dpy, eglSyncForCuda, 0);
    if (egl_status != EGL_TRUE) {
      printf("eglWaitSyncKHR failed\n");
      cleanup(FAILURE);
    }
    egl_status = eglDestroySyncKHR(dpy, eglSyncForCuda);
    if (egl_status != EGL_TRUE) {
      printf("EGL sync object destruction failed\n");
      cleanup(FAILURE);
    }
  }

  err = cudaGetValueMismatch();
  if (err != cudaSuccess) {
    printf("Value mismatch seen when using GPU sync\n");
    cleanup(FAILURE);
  }

  // Clean up CUDA writeResource
  status = cuGraphicsUnregisterResource(writeResource);
  if (status != CUDA_SUCCESS) {
    printf("Failed to unregister %d", status);
    cleanup(FAILURE);
  } else {
    printf("Unregistered writeResource. \n");
  }

  // Clean up CUDA readResource
  status = cuGraphicsUnregisterResource(readResource);
  if (status != CUDA_SUCCESS) {
    printf("Failed to unregister %d", status);
    cleanup(FAILURE);
  } else {
    printf("Unregistered readResource. \n");
  }
}

// Verifies the values in readSurface whether they are expected ones
// And writes value of newData into writeSurface
__global__ void verify_and_update_kernel(CUsurfObject write, CUsurfObject read,
                                         char expected, char newval, int width,
                                         int height) {
  unsigned int x = blockDim.x * blockIdx.x + threadIdx.x;
  unsigned int y = blockDim.y * blockIdx.y + threadIdx.y;

  if (x < width && y < height) {
    uchar4 check;
    surf2Dread(&check, read, x * 4, y);
    if (check.x != expected || check.y != expected || check.z != expected ||
        check.w != expected) {
      printf(
          "Mismatch found in values read[0]= %u read[1]= %u read[2]= %u "
          "read[3]= %u expected is %u\n",
          check.x, check.y, check.z, check.w, expected);
      numErrors++;
      return;
    }
    uchar4 data = make_uchar4(newval, newval, newval, newval);
    surf2Dwrite(data, write, x * 4, y);
  }
}

__global__ void getNumErrors(int *numErr) { *numErr = numErrors; }

extern "C" cudaError_t cudaGetValueMismatch() {
  int numErr_h;
  int *numErr_d = NULL;
  cudaError_t err = cudaSuccess;

  err = cudaMalloc(&numErr_d, sizeof(int));
  if (err != cudaSuccess) {
    printf("Cuda Main: cudaMemcpy failed with %s\n", cudaGetErrorString(err));
    cudaFree(numErr_d);
    return err;
  }

  getNumErrors<<<1, 1>>>(numErr_d);
  err = cudaDeviceSynchronize();
  if (err != cudaSuccess) {
    printf("Cuda Main: cudaDeviceSynchronize failed with %s\n",
           cudaGetErrorString(err));
  }
  err = cudaMemcpy(&numErr_h, numErr_d, sizeof(int), cudaMemcpyDeviceToHost);
  if (err != cudaSuccess) {
    printf("Cuda Main: cudaMemcpy failed with %s\n", cudaGetErrorString(err));
    cudaFree(numErr_d);
    return err;
  }
  err = cudaFree(numErr_d);
  if (err != cudaSuccess) {
    printf("Cuda Main: cudaFree failed with %s\n", cudaGetErrorString(err));
    return err;
  }
  if (numErr_h > 0) {
    return cudaErrorUnknown;
  }
  return cudaSuccess;
}

// Clean up state and exit. If status is SUCCESS, regression success is printed
// to stdout. This will happen if the glut timer is triggered. If status is
// anything else, the regression failure message is printed.
void cleanup(int status) {
  GLenum glErr = GL_NO_ERROR;
  cudaError cudaErr = cudaSuccess;
  int exitStatus = status;

  // Clean up GL
  if (*tex) {
    GL_SAFE_CALL_NO_CLEANUP(glDeleteTextures(2, tex), glErr);
  }

  // Print test status and exit
  if (glErr != GL_NO_ERROR || cudaErr != cudaSuccess) exitStatus = FAILURE;

  printStatus(exitStatus);

  cleanExit = 1;

  graphics_close_window();

  if (exitStatus == FAILURE) exit(EXIT_FAILURE);

  if (exitStatus == WAIVED) exit(EXIT_WAIVED);

  exit(0);
}

void exitHandler(void) {
  if (!cleanExit) {
    printf("&&&& EGLSync_CUDAEvent_Interop unexpected failure \n");
    printStatus(FAILURE);
  }
}

// Print test success or fail for regression testing
void printStatus(int status) {
  switch (status) {
    case SUCCESS:
      printf("&&&& EGLSync_CUDAEvent_Interop PASSED\n");
      break;
    case WAIVED:
      printf("&&&& EGLSync_CUDAEvent_Interop WAIVED\n");
      break;
    default:
      printf("&&&& EGLSync_CUDAEvent_Interop FAILED\n");
      break;
  }
  fflush(stdout);
}