Skip to content

Example

Andrew Chapman edited this page Nov 11, 2020 · 4 revisions
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <math.h>
#include "rocblas.h"

using namespace std;

int main()
{
    rocblas_int N = 10240;
    float alpha = 10.0;

    vector<float> hx(N);
    vector<float> hz(N);
    float* dx;
    float tolerance = 0, error;

    rocblas_handle handle;
    rocblas_create_handle(&handle);

    // allocate memory on device
    hipMalloc(&dx, N * sizeof(float));

    // Initial Data on CPU,
    srand(1);
    for( int i = 0; i < N; ++i )
    {
        hx[i] = rand() % 10 + 1;  //generate a integer number between [1, 10]
    }

    // save a copy in hz 
    hz = hx;

    hipMemcpy(dx, hx.data(), sizeof(float) * N, hipMemcpyHostToDevice);

    rocblas_sscal(handle, N, &alpha, dx, 1);

    // copy output from device memory to host memory
    hipMemcpy(hx.data(), dx, sizeof(float) * N, hipMemcpyDeviceToHost);

    // verify rocblas_scal result
    for(rocblas_int i=0;i<N;i++)
    {
        error = fabs(hz[i] * alpha - hx[i]);
        if(error > tolerance)
        {
          printf("error in element %d: CPU=%f, GPU=%f ", i, hz[i] * alpha, hx[i]);
          break;
        }
    }

    if(error > tolerance)
    {
        printf("SCAL Failed !\n");
    }
    else
    {
        printf("SCAL Success !\n");
    }

    hipFree(dx);
    rocblas_destroy_handle(handle);
    return 0;
}

Paste the above code into the file rocblas_sscal_example.cpp

Use hipcc Compiler:

The recommend host compiler is hipcc. To use hipcc you will need to add /opt/rocm/bin to your path with the following:

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

The following makefile can be used to build the executable.

The Makefile assumes that rocBLAS is installed in the default location /opt/rocm/rocblas. If you have rocBLAS installed in your home directory in ~/rocBLAS/build/release/rocblas-install/rocblas then edit Makefile and change /opt/rocm/rocblas to ~/rocBLAS/build/release/rocblas-install/rocblas.

You may need to give the location of the library with

export LD_LIBRARY_PATH=/opt/rocm/rocblas/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

Run the executable with the command

./rocblas_sscal_example
# Makefile assumes rocBLAS is installed in /opt/rocm/rocblas

ROCBLAS_INSTALL_DIR=/opt/rocm/rocblas
ROCBLAS_INCLUDE=$(ROCBLAS_INSTALL_DIR)/include
ROCBLAS_LIB_PATH=$(ROCBLAS_INSTALL_DIR)/lib
ROCBLAS_LIB=rocblas
HIP_INCLUDE=/opt/rocm/hip/include
LDFLAGS=-L$(ROCBLAS_LIB_PATH) -l$(ROCBLAS_LIB)
LD=hipcc
CFLAGS=-I$(ROCBLAS_INCLUDE) -I$(HIP_INCLUDE)
CPP=hipcc
OBJ=rocblas_sscal_example.o
EXE=rocblas_sscal_example

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

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

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

Use g++ Compiler:

Use the Makefile below

ROCBLAS_INSTALL_DIR=/opt/rocm/rocblas
ROCBLAS_INCLUDE=$(ROCBLAS_INSTALL_DIR)/include
ROCBLAS_LIB_PATH=$(ROCBLAS_INSTALL_DIR)/lib
ROCBLAS_LIB=rocblas
ROCM_INCLUDE=/opt/rocm/include
LDFLAGS=-L$(ROCBLAS_LIB_PATH) -l$(ROCBLAS_LIB) -L/opt/rocm/lib -lhip_hcc
LD=g++
CFLAGS=-I$(ROCBLAS_INCLUDE) -I$(ROCM_INCLUDE) -D__HIP_PLATFORM_HCC__
CPP=g++
OBJ=rocblas_sscal_example.o
EXE=rocblas_sscal_example

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

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

clean:
	rm -f $(EXE) $(OBJ)
Clone this wiki locally