Skip to content

Add a workaround for a NVIDIA nvc/nvc++ compiler bug

Junchao Zhang requested to merge jczhang/feature-nvc-bug-workaround into release

Reported-by: Jonathan D. Halverson halverson@princeton.edu

The bug happens with NVHPC SDK 21.11 (December 2021). See https://lists.mcs.anl.gov/pipermail/petsc-users/2021-December/045158.html

A test case and the workaround are as follows.

/*
$ nvc --version

nvc 21.11-0 64-bit target on x86-64 Linux -tp haswell
NVIDIA Compilers and Tools
Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES.  All rights reserved.

$ nvc -O0 -o test test.c
$ ./test
sizeof(MatEntry2)=16
Segmentation fault (core dumped)

*/

#include <stdlib.h>
#include <stdio.h>
#include <complex.h>

typedef double _Complex PetscScalar;
typedef struct {
  int            row;
  PetscScalar    *valaddr;
} MatEntry2;

int main(int arc, char** argv)
{
  int         i=2;
  MatEntry2   *Jentry2 = (MatEntry2*)malloc(64*sizeof(MatEntry2));
  PetscScalar a=1, b=1;


  printf("sizeof(MatEntry2)=%lu\n",sizeof(MatEntry2));
  Jentry2[2].valaddr = (PetscScalar*)malloc(16*sizeof(PetscScalar));

  *(Jentry2[i].valaddr) = a*b; // segfault
  // otherwise works with
  //   PetscScalar *p = Jentry2[i].valaddr;
  //   *p = a*b;

  free(Jentry2[2].valaddr);
  free(Jentry2);
  return 0;
}
Edited by Junchao Zhang

Merge request reports