Rate This Document
Findability
Accuracy
Completeness
Readability

kupl_parallel_for_reduce

Perform parallel reduction.

Interface Definition

int kupl_parallel_for_reduce(kupl_parallel_for_desc_t *desc, kupl_pf_reduce_func_t func, void *args,

kupl_reduce_args_t *rd_args);

Parameters

Table 1 Parameter definition

Parameter

Type

Description

Input/Output

desc

kupl_parallel_for_desc_t *

Description of a parallel iteration task, which is a pointer to the kupl_parallel_for_desc_t structure. For details, see the definition in the section "kupl_parallel_for."

Input

func

kupl_pf_reduce_func_t

Parallel reduction function, which must be defined in the following format of "void (*kupl_pf_reduce_func_t)(kupl_nd_range_t *nd_range, void *args, int tid, int tnum,

kupl_reduce_args_t *rd_args)". nd_range indicates the loop region actually executed by the function after internal processing via kupl_parallel_for, args indicates the arguments passed to this structure, tid and tnum indicate the ID of the thread where the current function is executed and the total number of threads, respectively, and rd_args indicates the thread replica of the reduction parameter.

For details, see the definition of kupl_reduce_args_t.

Input

args

void *

Arguments to be passed to func.

Input

rd_args

kupl_reduce_args_t *

Reduction parameter, which defines the total number of reduction operations, data type of the reduction operation, reduction operation type, and reduction data address.

Input/Output

Table 2 kupl_reduce_args_t structure definition

Parameter

Type

Description

num

int

Number of reduction operations.

items

kupl_reduce_item_t *

Pointer to the specific reduction operation definition. For details, see the following description.

Table 3 kupl_reduce_item_t structure definition

Parameter

Type

Description

buffer

void *

Data address of the reduction operation.

type

kupl_datatype_t

Data type of the reduction operation, including:

KUPL_DATATYPE_INT,

KUPL_DATATYPE_FLOAT,

KUPL_DATATYPE_DOUBLE,

KUPL_DATATYPE_FLOAT_COMPLEX,

KUPL_DATATYPE_DOUBLE_COMPLEX

op

kupl_reduce_op_t

Reduction operation type, including:

KUPL_RD_ADD,

KUPL_RD_SUB,

KUPL_RD_MAX,

KUPL_RD_MIN

Return Value

  • Success: KUPL_OK
  • Failure: KUPL_ERROR

Examples

One-dimensional loop:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <pthread.h>
#include "kupl.h"
static inline void task_in_loop_reduce(kupl_nd_range_t *nd_range, void *args, int tid, int tnum, kupl_reduce_args_t *rd_args)
{
    int *data = (int *)args;
    int localsum = 0;
    for (int i = nd_range->nd_range[0].lower; i < nd_range->nd_range[0].upper; i += nd_range->nd_range[0].step) {
        localsum += data[i];
    }
    *(int *)rd_args->items[0].buffer += localsum;
}

int main()
{
    const int n = 100;
    int data[n];
    for (int i = 0; i < n; i++) {
        data[i] = i + 1;
    }
    kupl_nd_range_t range;
    KUPL_1D_RANGE_INIT(range, 0, n);


    kupl_parallel_for_desc_t desc = {
        .field_mask = KUPL_PARALLEL_FOR_DESC_FIELD_DEFAULT,
        .range = &range,
        .egroup = NULL,
        .concurrency = 4,
        .policy = KUPL_LOOP_POLICY_STATIC
    };
    int sum_int = 0;
    kupl_reduce_item_t param_int[1] = {{ .buffer = &sum_int, .type = KUPL_DATATYPE_INT, .op = KUPL_RD_ADD }};
    kupl_reduce_args_t rd_args_int = { .num = 1, .items = param_int };
    kupl_parallel_for_reduce(&desc, task_in_loop_reduce, data, &rd_args_int);
    printf("sum: %d\n", sum_int);
}

The execution result is as follows:

sum: 5050

The preceding example demonstrates the process of KUPL parallel reduction and calculates the sum of all values from 1 to 100 in the data array.