akhildevelops / cudaz

A Zig Cuda wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cimport.zig generation error for vector_types.h header file

glyh opened this issue · comments

$ zig build -DCUDA_PATH=/opt/cuda
error: invalid option: -DCUDA_PATH
  access the help menu with 'zig build -h'
error: the following build command failed with exit code 1:
/home/lyh/Documents/Learn/Essay/HVM-GPU/zig-cache/o/3d2159c46b9c9a19ba87dd9a51346c12/build /home/lyh/.zvm/master/zig /home/lyh/Documents/Learn/Essay/HVM-GPU /home/lyh/Documents/Learn/Essay/HVM-GPU/zig-cache /home/lyh/.cache/zig --seed 0x508410eb -Z888408d032b7a37e -DCUDA_PATH=/opt/cuda run

here's my build.zig:

//build.zig
const std = @import("std");

pub fn build(b: *std.Build) !void {
    // exe points to main.zig that uses cudaz
    const exe = b.addExecutable(.{ .name = "main", .root_source_file = .{ .path = "src/main.zig" }, .target = b.host });

    // Point to cudaz dependency
    const cudaz_dep = b.dependency("cudaz", .{});

    // Fetch and add the module from cudaz dependency
    const cudaz_module = cudaz_dep.module("cudaz");
    exe.root_module.addImport("cudaz", cudaz_module);

    // Dynamically link to libc, cuda, nvrtc
    exe.linkLibC();
    exe.linkSystemLibrary("cuda");
    exe.linkSystemLibrary("nvrtc");

    // Run binary
    const run = b.step("run", "Run the binary");
    const run_step = b.addRunArtifact(exe);
    run.dependOn(&run_step.step);
}

And my zon:

.{
    .name = "HVM-GPU",
    // This is a [Semantic Version](https://semver.org/).
    // In a future version of Zig it will be used for package deduplication.
    .version = "0.0.0",

    // This field is optional.
    // This is currently advisory only; Zig does not yet do anything
    // with this value.
    //.minimum_zig_version = "0.11.0",

    // This field is optional.
    // Each dependency must either provide a `url` and `hash`, or a `path`.
    // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
    // Once all dependencies are fetched, `zig build` no longer requires
    // internet connectivity.
    .dependencies = .{
        .cudaz = .{
            .url = "https://github.com/akhildevelops/cudaz/tarball/master",
            .hash = "12207f31e4b03f69b625ca308faa4e164393f1a33860e3046a72f92c9fc5bd979c4f",
        },
    },
    .paths = .{
        // This makes *all* files, recursively, included in this package. It is generally
        // better to explicitly list the files and directories instead, to insure that
        // fetching from tarballs, file system paths, and version control all result
        // in the same contents hash.
        "",
        // For example...
        //"build.zig",
        //"build.zig.zon",
        //"src",
        //"LICENSE",
        //"README.md",
    },
}

How are you building / running your application ? Share the zig command

zig build -DCUDA_PATH=/opt/cuda

Looks like running the seed command directly I got

$ zig --seed 0x1d1d4a65 -Zde76dbf14f7e1309 -DCUDA_PATH=/opt/cuda
info: Usage: zig [command] [options]

Commands:

  build            Build project from build.zig
  fetch            Copy a package into global cache and print its hash
  init             Initialize a Zig package in the current directory

  build-exe        Create executable from source or object files
  build-lib        Create library from source or object files
  build-obj        Create object from source or object files
  test             Perform unit testing
  run              Create executable and run immediately

  ast-check        Look for simple compile errors in any set of files
  fmt              Reformat Zig source into canonical form
  reduce           Minimize a bug report
  translate-c      Convert C code to Zig code

  ar               Use Zig as a drop-in archiver
  cc               Use Zig as a drop-in C compiler
  c++              Use Zig as a drop-in C++ compiler
  dlltool          Use Zig as a drop-in dlltool.exe
  lib              Use Zig as a drop-in lib.exe
  ranlib           Use Zig as a drop-in ranlib
  objcopy          Use Zig as a drop-in objcopy
  rc               Use Zig as a drop-in rc.exe

  env              Print lib path, std path, cache directory, and version
  help             Print this help and exit
  std              View standard library documentation in a browser
  libc             Display native libc paths file or validate one
  targets          List available compilation targets
  version          Print version number and exit
  zen              Print Zen of Zig and exit

General Options:

  -h, --help       Print command-specific usage

error: unknown command: --seed

Also, please note which version of zig and cuda are required for the package. I'm on v0.12.0-dev.3518+d2be725e4 and CUDA 11.8.0

With a zig build run here's what I got:

run
└─ run main
   └─ zig build-exe main Debug native 2 errors
/tmp/a/zig-cache/o/e6313d38358f34147d7d6a956435d3c5/cimport.zig:3025:84: error: use of undeclared identifier 'struct___device_builtin__'
pub inline fn __cuda_builtin_vector_align8(tag: anytype, members: anytype) @TypeOf(struct___device_builtin__ ++ __align__(@as(c_int, 8)) ++ @import("std").mem.zeroInit(tag, .{members})) {
                                                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/lyh/.cache/zig/p/12207f31e4b03f69b625ca308faa4e164393f1a33860e3046a72f92c9fc5bd979c4f/src/c.zig:5:20: error: C import failed: AnalysisFail
pub const curand = @cImport(@cInclude("curand.h"));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    curand: /home/lyh/.cache/zig/p/12207f31e4b03f69b625ca308faa4e164393f1a33860e3046a72f92c9fc5bd979c4f/src/error.zig:7:17
    ErrorsToEnum: /home/lyh/.cache/zig/p/12207f31e4b03f69b625ca308faa4e164393f1a33860e3046a72f92c9fc5bd979c4f/src/error.zig:20:9
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 2 compilation errors:
/home/lyh/.zvm/master/zig build-exe -I/opt/cuda/targets/x86_64-linux/include -L/opt/cuda/targets/x86_64-linux/lib -lcuda -I/opt/cuda/targets/x86_64-linux/include -L/opt/cuda/targets/x86_64-linux/lib -lnvrtc -ODebug --dep cudaz -Mroot=/tmp/a/src/main.zig -I /opt/cuda/include -L /opt/cuda/lib -L /opt/cuda/lib/x64 -L /opt/cuda/lib/Win32 -L /opt/cuda/lib/x86_64 -L /opt/cuda/lib/x86_64-linux-gnu -L /opt/cuda/lib64 -L /opt/cuda/lib64/stubs -L /opt/cuda/targets/x86_64-linux -L /opt/cuda/targets/x86_64-linux/lib -L /opt/cuda/targets/x86_64-linux/lib/stubs -Mcudaz=/home/lyh/.cache/zig/p/12207f31e4b03f69b625ca308faa4e164393f1a33860e3046a72f92c9fc5bd979c4f/src/lib.zig -lc --cache-dir /tmp/a/zig-cache --global-cache-dir /home/lyh/.cache/zig --name main --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run main transitive failure
   └─ zig build-exe main Debug native 2 errors
error: the following build command failed with exit code 1:
/tmp/a/zig-cache/o/f0a537fcff89c2e8364e3e2fbb1b262c/build /home/lyh/.zvm/master/zig /tmp/a /tmp/a/zig-cache /home/lyh/.cache/zig --seed 0x197c0b26 -Z62d561275b795085 run

And here' a link to vector_types.h of cuda, containing the error defintion: https://gist.github.com/glyh/ddde942541e9492eaa0f8a7e5067c68e#file-vector_types-h-L80

I'm can reproduce the same issue. I think there's some problem with zig parsing the header file.

Can you check if the zig binary at this commit https://github.com/ziglang/zig/tree/1e67f5021159ed1d9888cf2d0b9f04ef73222f7d works for you ?

You can follow the build steps mentioned to create the binary.

Is that commit in master? I managed to reproduce with master(0.12.0-dev.3522+b88ae8dbd) from https://ziglang.org/download/ , which is a build from yesterday(2024-04-03). Also, what obout the error message I got with -DCUDA_PATH? These seem to be 2 different errors.

-DCUDA_PATH is a build argument for cudaz library but not for the application using it. If you want to mention the custom cuda path from application, then mention the path in the build.zig file like below:

//build.zig
const std = @import("std");

pub fn build(b: *std.Build) !void {
    ...
    ...
    // Point to cudaz dependency
    const cudaz_dep = b.dependency("cudaz", .{ .CUDA_PATH = @as([]const u8, "/usr/lib/cuda") });
    ...
   ...
}

Okay, so is that commit on master?

Yes

What should I do now then?

I've been able to fix by fiddling with zig codegen with respect to headerfiles.

Try this branch: https://github.com/akhildevelops/cudaz/tree/temp-fix

But you need to manually edit cimport.zig code generated by zig.

Initially it generates below code in ....../cimport.zig in zig-cache directory

pub inline fn __cuda_builtin_vector_align8(tag: anytype, members: anytype) @TypeOf(struct___device_builtin__ ++ __align__(@as(c_int, 8)) ++ @import("std").mem.zeroInit(tag, .{members})) {
    _ = &tag;
    _ = &members;
    return struct___device_builtin__ ++ __align__(@as(c_int, 8)) ++ @import("std").mem.zeroInit(tag, .{members});
}

You have to remove struct_ to make it work in cimport.zig file. Now the modified version is as below:

pub inline fn __cuda_builtin_vector_align8(tag: anytype, members: anytype) @TypeOf(__device_builtin__ ++ __align__(@as(c_int, 8)) ++ @import("std").mem.zeroInit(tag, .{members})) {
    _ = &tag;
    _ = &members;
    return __device_builtin__ ++ __align__(@as(c_int, 8)) ++ @import("std").mem.zeroInit(tag, .{members});
}

I'll file an issue shortly in zig repo

ziglang/zig#19571 for reference

Fixed this issue. Please check

I'm on 0.13.0-dev.46+3648d7df1 (master), and the builder seems to be freezing:

zig build run
steps [3/7] zig build-exe hello_cuda_z Debug native... LLVM Emit Object...         

And it stucks forever until I press Ctrl+C.

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0

Cudaz will only support 0.12.0 no nightly