Seg Fault when returning from host function

Here’s example of code:

func.h

#include <glm/vec3.hpp>
using namespace glm;

/*struct vec3
{
public:
    float x,y,z;
    vec3(float v) : x(v), y(v), z(v) {}
};*/

vec3 function();

func.cu

#include "func.h"

vec3 function()
{
    return vec3(0.0f);
}

main.cpp

#include <iostream>
#include "func.h"

int main()
{
    vec3 vect = function();
    std::cout << "vector = " << "(" << vect.x << ", " << vect.y << ", " << vect.z << ")" << std::endl;
    return 0;
}

I’m trying to link library that is compiled with nvcc to ordinary c++ code. Compilation and linking goes normally, but i’m getting seg fault in function that returns glm::vec3 type. This doesn’t occur when i’m using my own vec3 type (in file func.h) or any other type. So is something wrong with glm vector or am I doing something bad?

Build steps:

nvcc -o func.o -c func.cu
nvcc -lib -o func.a func.o
/usr/local/cuda/bin/g++ -o main.o -c main.cpp
/usr/local/cuda/bin/g++ main.o func.a -o test -L/usr/local/cuda-9.1/lib64 -lcudart

First of all this seems a bit wierd:

/usr/local/cuda/bin/g++

You have a g++ installed or symlinked in your cuda bin directory? Not sure why that would be necessary.

second, when I compile and run your code as-is, I get:

vector = (0, 0, 0)

(and no seg fault)

CUDA 9.1, CentOS 7.4, installed glm-devel, gnu 4.8.5 (default)

I’d be a little worried that nvcc is using a g++ that is different than the one you are using explicitly

full test case:

$ g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat func.h
#include <glm/vec3.hpp>
using namespace glm;

/*struct vec3
 * {
 * public:
 *     float x,y,z;
 *         vec3(float v) : x(v), y(v), z(v) {}
 *         };*/

vec3 function();
$ cat func.cu
#include "func.h"

vec3 function()
{
    return vec3(0.0f);
}
$ cat main.cpp
#include <iostream>
#include "func.h"

int main()
{
    vec3 vect = function();
    std::cout << "vector = " << "(" << vect.x << ", " << vect.y << ", " << vect.z << ")" << std::endl;
    return 0;
}
$ nvcc -o func.o -c func.cu
$ nvcc -lib -o func.a func.o                                   
$ g++ -o main.o -c main.cpp                                    
$ g++ main.o func.a -o test -L/usr/local/cuda-9.1/lib64 -lcudart
$ ./test
vector = (0, 0, 0)
$

I have g++ version 6.4 symlinked in my cuda bin, and i guess this is the one that nvcc uses as well. I also have g++ version 7.2 installed, I’ve compiled with it and it gave me the same result. Could it somehow miss a seg fault in your case?

The highest gcc supported version I see for CUDA 9.1 is 6.3:

http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html

symlinking a gcc version in the cuda bin directory is not the suggested way to override the default gcc.

You should either include the gcc compiler first in your PATH, or you should use the -ccbin switch when compiling.

I don’t really know what the problem is in your case, but I suspect you will not see the problem if you use a proper/supported config.

Yes, looks like that was the case. Thank you very much!