Skip to content

Example C code

Nico edited this page Jun 29, 2020 · 5 revisions
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <rocsparse.h>
#include <hip/hip_runtime_api.h>

using namespace std;

int main()
{
    rocsparse_int N   = 10240;
    rocsparse_int nnz = 256;
    float alpha       = 10.0f;
    float tolerance   = 1e-8f;

    vector<rocsparse_int> hx_ind(nnz);
    vector<float> hx_val(nnz);
    vector<float> hy(N);

    // Allocate memory on the device
    rocsparse_int* dx_ind;
    float* dx_val;
    float* dy;

    hipMalloc(&dx_ind, nnz * sizeof(rocsparse_int));
    hipMalloc(&dx_val, nnz * sizeof(float));
    hipMalloc(&dy, N * sizeof(float));

    // Initial Data on CPU,
    srand(1);

    for(rocsparse_int i = 0; i < nnz; ++i)
    {
        hx_ind[i] = i * 40;
        hx_val[i] = rand() % 10 + 1; // Generate an integer number between [1, 10]
    }

    for(rocsparse_int i = 0; i < N; ++i)
    {
        hy[i] = rand() % 10 + 1; // Generate an integer number between [1, 10]
    }

    // Copy data to device
    hipMemcpy(dx_ind, hx_ind.data(), sizeof(rocsparse_int) * nnz, hipMemcpyHostToDevice);
    hipMemcpy(dx_val, hx_val.data(), sizeof(float) * nnz, hipMemcpyHostToDevice);
    hipMemcpy(dy, hy.data(), sizeof(float) * N, hipMemcpyHostToDevice);

    // Initialize rocSPARSE
    rocsparse_handle handle;
    rocsparse_create_handle(&handle);

    // Run saxpyi on device
    rocsparse_saxpyi(handle, nnz, &alpha, dx_val, dx_ind, dy, rocsparse_index_base_zero);

    // Copy output from device memory to host memory
    vector<float> result(N);
    hipMemcpy(result.data(), dy, sizeof(float) * N, hipMemcpyDeviceToHost);

    // Verify rocsparse_axpyi result
    for(rocsparse_int i = 0; i < nnz; ++i)
    {
        hy[hx_ind[i]] += alpha * hx_val[i];
    }

    float error;
    for(rocsparse_int i = 0; i < N; ++i)
    {
        error = fabs(hy[i] - result[i]);
        if(error > tolerance)
        {
            fprintf(stderr, "Error in element %d: CPU=%f, GPU=%f\n", i, hy[i], result[i]);
            break;
        }
    }

    if(error > tolerance)
    {
        printf("axpyi test failed!\n");
    }
    else
    {
        printf("axpyi test passed!\n");
    }

    hipFree(dx_ind);
    hipFree(dx_val);
    hipFree(dy);

    rocsparse_destroy_handle(handle);

    return 0;
}

=================================

Use hipcc Compiler:

The recommended host compiler is [hipcc] (https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/). You may need to add /opt/rocm/bin to your path with the following command:

export PATH=$PATH:/opt/rocm/bin

If the above code is pasted into a file rocsparse_saxpyi_example.cpp, the following makefile can be used to build an executable. You will need to give the location of the library with

export LD_LIBRARY_PATH=~/repos/rocSPARSE/build/library-package/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

Run the executable with the command

./rocsparse_saxpyi_example
# Makefile assumes rocSPARSE is installed in $(HOME)/repos/rocSPARSE/build/library-package
ROCSPARSE_INSTALL_DIR=$(HOME)/repos/rocSPARSE/build/library-package
ROCSPARSE_INCLUDE=$(ROCSPARSE_INSTALL_DIR)/include
ROCSPARSE_LIB_PATH=$(ROCSPARSE_INSTALL_DIR)/lib
ROCSPARSE_LIB=rocsparse
HIP_INCLUDE=/opt/rocm/hip/include
LDFLAGS=-lm -L$(ROCSPARSE_LIB_PATH) -l$(ROCSPARSE_LIB)
LD=hipcc
CFLAGS=-I$(ROCSPARSE_INCLUDE) -I$(HIP_INCLUDE)
CPP=hipcc
OBJ=rocsparse_saxpyi_example.o
EXE=rocsparse_saxpyi_example

%.o: %.cpp
        $(CPP) -c -o $@ $< $(CFLAGS)

$(EXE) : $(OBJ)
        $(LD) $(LDFLAGS) $(OBJ) -o $@ 

clean:
        rm -f $(EXE) $(OBJ)

=================================

Use standard g++ Compiler:

Add -D__HIP_PLATFORM_HCC__ -I/opt/rocm/include into your compiler flag. Tell g++ your librocsparse.so PATH (by default in /opt/rocm/lib64) and libamdhip64 path (by default in /opt/rocm/lib) Tell your rocsparse.h header (by default in /opt/rocm/include)

The whole command is

g++ rocsparse_saxpyi_example.cpp -D__HIP_PLATFORM_HCC__ -I/opt/rocm/include -L/opt/rocm/lib -lamdhip64 -L/opt/rocm/lib64 -lrocsparse

Clone this wiki locally