# Utilities

## HPPL Base

### hl_base.h

Defines

`HL_FLOAT_MAX`

  HPPL data type: real (float or double)

  if real == float

  HL_FLOAT_MAX: 3.40282347e+38F

  HL_FLOAT_MIN: 1.17549435e-38F

`HL_FLOAT_MIN`

  if real == double

  HL_FLOAT_MAX: 1.7976931348623157e+308

  HL_FLOAT_MIN: 2.2250738585072014e-308

`DIVUP`(x, y)

  DIVUP(x, y) is similar to ceil(x / y).

  Note
    For CUDA, DIVUP will be used to specify the size of blockDim.

Typedefs

typedef struct _hl_matrix_s *`hl_matrix_s`

typedef struct _hl_sparse_matrix_s *`hl_sparse_matrix_s`

Enums

enum `hl_stream_t`

  HPPL CUDA Stream.

  HPPL is an internal high performance parallel computing library for high-level neural network routines, which can support many heterogeneous compute architectures, such as GPU, FPGA, etc.

  Note
    Each thread can use HPPL_STREAM_* after calling hl_init. HPPL_STREAM_DEFAULT is HPPL default stream.

  Values:

  `HPPL_STREAM_DEFAULT` = 0
  `HPPL_STREAM_1` = 1
  `HPPL_STREAM_2` = 2
  `HPPL_STREAM_3` = 3
  `HPPL_STREAM_4` = 4
  `HPPL_THREAD_STREAM_1` = 5
  `HPPL_THREAD_STREAM_2` = 6
  `HPPL_THREAD_STREAM_3` = 7
  `HPPL_THREAD_STREAM_4` = 8
  `HPPL_STREAM_END`

enum `hl_activation_mode_t`

  HPPL activation mode.

  Values:

  `HL_ACTIVATION_SIGMOID` = 0
  `HL_ACTIVATION_RELU` = 1
  `HL_ACTIVATION_TANH` = 2
  `HL_ACTIVATION_LINEAR` = 3
  `HL_ACTIVATION_END`

enum `hl_trans_op_t`

  Transpose type.

  Values:

  `HPPL_OP_N` = 0
  `HPPL_OP_T` = 1
  `HPPL_OP_END`

enum `hl_matrix_value_t`

  Sparse matrix value type.

  Values:

  `HL_NO_VALUE` = 0
  `HL_FLOAT_VALUE` = 1
  `HL_VALUE_END`

enum `hl_matrix_format_t`

  HPPL matrix format.

  Values:

  `HL_SPARSE_CSR` = 0
  `HL_SPARSE_CSC` = 1
  `HL_SPARSE_END`

struct `hl_lstm_value`
  #include <hl_base.h>

  Lstm value.

  Parameters
    - `gateValue` -

      input value.

    - `prevStateValue` -

      previous state value.

    - `stateValue` -

      state value.

    - `stateActiveValue` -

      state active value.

    - `outputValue` -

      output value.

  Public Members

  real *`gateValue`
  real *`prevStateValue`
  real *`stateValue`
  real *`stateActiveValue`
  real *`outputValue`
  real *`checkIg`
  real *`checkFg`
  real *`checkOg`

struct `hl_lstm_grad`
  #include <hl_base.h>

  Lstm gradient.

  Parameters
    - `gateGrad` -

      input gradient.

    - `prevStateGrad` -

      previous state gradient.

    - `stateGrad` -

      state gradient.

    - `stateActiveGrad` -

      state active gradient.

    - `outputGrad` -

      output gradient.

  Public Members

  real *`gateGrad`
  real *`prevStateGrad`
  real *`stateGrad`
  real *`stateActiveGrad`
  real *`outputGrad`
  real *`checkIgGrad`
  real *`checkFgGrad`
  real *`checkOgGrad`

struct `hl_gru_value`
  #include <hl_base.h>

  Gru value.

  Parameters
    - `gateWeight` -

      gate weight (updateGate + resetGate).

    - `stateWeight` -

      frame state weight.

    - `gateValue` -

      gate value results.

    - `resetOutputValue` -

      resetOutput value.

    - `outputValue` -

      output value.

    - `prevOutValue` -

      previous output value.

  Public Members

  real *`gateWeight`
  real *`stateWeight`
  real *`gateValue`
  real *`resetOutputValue`
  real *`outputValue`
  real *`prevOutValue`

struct `hl_gru_grad`
  #include <hl_base.h>

  Gru gradient.

  Parameters
    - `gateWeightGrad` -

      gate weight gradient.

    - `stateWeightGrad` -

      frame state weight gradient.

    - `gateGrad` -

      gate gradient results.

    - `resetOutputGrad` -

      resetOutput gradient.

    - `outputGrad` -

      output gradient.

    - `prevOutGrad` -

      previous output gradient.

  Public Members

  real *`gateWeightGrad`
  real *`stateWeightGrad`
  real *`gateGrad`
  real *`resetOutputGrad`
  real *`outputGrad`
  real *`prevOutGrad`

struct `_hl_sparse_matrix_s`
  #include <hl_base.h>

  HPPL sparse matrix.

  Parameters
    - `matrix` -

      sparse matrix.

    - `format` -

      matrix format.

    - `type` -

      the type of matrix values.

    - `rows` -

      matrix rows.

    - `cols` -

      matrix columns.

    - `nnz` -

      nonzero values of sparse matrix.

  Public Members

  hl_matrix_s `matrix`
  hl_matrix_format_t `format`
  hl_matrix_value_t `type`
  int `rows`
  int `cols`
  size_t `nnz`

## Timer

### hl_time.h

Functions

int64_t `getCurrentTimeStick`(void)

  High resolution timer.

  Return
    int64_t the representation value of the object as a count of periods, which are not necessarily seconds.
  Note
    It is used to generate random perturbation parameters.

## Thread Resource

### hl_thread.ph

Defines

`HL_THREAD_PH_`

Typedefs

typedef struct _hl_thread_resource *`hl_thread_resource`

Functions

void `hl_cudnn_init`(cudnnHandle_t *cudnn_handle, cudaStream_t stream)

  Initialize cudnn.

  Parameters
    - `cudnn_handle` -

      Cudnn handle.

    - `stream` -

      Cudnn stream.

void `hl_cublas_init`(cublasHandle_t *cublas_handle, cudaStream_t stream)

  Initialize cublas.

  Parameters
    - `cublas_handle` -

      Cublas handle.

    - `stream` -

      Cuda stream.

void `hl_cudnn_desc_init`(cudnnTensorDescriptor_t *cudnn_desc)

  Initialize cudnn tensor descriptor.

  Parameters
    - `cudnn_desc` -

      Cudnn tensor descriptor.

Variables

`__thread _hl_thread_resource t_resource`

  thread resource.

struct `_hl_thread_resource`

  Thread resource structure.

  Parameters
    - `stream[HPPL_STREAM_END]` -

      Stream for thread.

    - `handle` -

      Cublas Handle.

    - `gen` -

      Curand Generator.

    - `cudnn_handle` -

      Cudnn handle.

    - `cudnn_desc` -

      Cudnn image descriptor.

    - `*gen_mutex` -

      Gen lock.

    - `*gpu_mem` -

      HPPL GPU Memory.

    - `*cpu_mem` -

      HPPL CPU Memory.

    - `event` -

      gpu_mem event.

    - `device` -

      Thread device context.

    - `major` -

      Compute capability.

    - `is_init` -

      Thread init or not.

  Public Members

  cudaStream_t `stream`[HPPL_STREAM_END]
  cublasHandle_t `handle`
  curandGenerator_t `gen`
  cudnnHandle_t `cudnn_handle`
  cudnnTensorDescriptor_t `cudnn_desc`
  pthread_mutex_t *`gen_mutex`
  real *`gpu_mem`
  real *`cpu_mem`
  cudaEvent_t `event`
  int `device`
  int `major`
  bool `is_init`