Rate This Document
Findability
Accuracy
Completeness
Readability

ParallelFor

Perform parallel iterations:

HmppResult HMPP_ParallelFor (int32_t numTasks, void *arg, function func);

Multiple tasks to be executed can be encapsulated in HmppResult(*function)(int32_t i, void *arg). For details, see the example.

Parameters

Parameter

Description

Value Range

Input/Output

numTasks

Number of tasks

The value is greater than 0. If the value is less than 0, no exceptions are reported and such a value indicates that no tasks are created.

Input

arg

Pointer to the parameter value of func

The value cannot be NULL.

Input

func

Pointer to the function (task) to be executed

The value cannot be NULL.

Input

Return Value

  • Success: HMPP_STS_NO_ERR
  • Failure: An error code is returned.

Error Codes

Error Code

Description

HMPP_STS_NULL_PTR_ERR

The arg or func pointer is NULL.

An error code will be returned if the multi-thread mode is enabled. You can set the error code in the user-defined function.

Example

#define LEN 20
 
void  ParallelFor_Example()
{
    typedef struct {
        int32_t val[LEN];
        int32_t rangeLen;
    }Arg;
    // Operation of this function: Obtaining the absolute value of a segment in an array
    auto func = [] (int32_t i, void *arg) -> HmppResult {
        Arg *args = (Arg *)arg;
        int32_t st = i * args->rangeLen;
        int32_t ed = st + args->rangeLen;
        for (int32_t k = st; k < ed; ++k) {
            args->val[k] = abs(args->val[k]);
        }
        return HMPP_STS_NO_ERR;
    };

    Arg arg;
    arg.rangeLen = 5;
    for (int32_t i = 0; i < LEN; ++i) {
        arg.val[i] = -(i + 1);
    }

    HmppResult result = HMPP_ParallelFor(LEN / arg.rangeLen, (void*)&arg, func);

    for (int32_t i = 0; i < LEN; ++i) {
        printf("%d\n", arg.val[i]);
    }
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20