aclnnCeil&aclnnInplaceCeil
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| √ | |
| √ |
Function
Description: Returns the result of rounding up each element in the input tensor.
Formula:
Function Prototype
aclnnCeil and aclnnInplaceCeil implement the same function. The differences in usage are as follows. Select the appropriate operator based on your actual scenario.
aclnnCeil: Create a new output tensor object to store the computation result.
aclnnInplaceCeil: No need to create a new output tensor object. The computation result is stored directly in the memory of the input tensor.
Each operator uses the two-phase API. You must first call the "aclnnCeilGetWorkspaceSize" or "aclnnInplaceCeilGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that encapsulates the operator computation process, and then call the "aclnnCeil" or "aclnnInplaceCeil" API to perform the computation.
aclnnStatus aclnnCeilGetWorkspaceSize(
const aclTensor* self,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnCeil(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnStatus aclnnInplaceCeilGetWorkspaceSize(
aclTensor* selfRef,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceCeil(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnCeilGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data type Data format Shape Non-contiguous tensor self (aclTensor*) Input Input tensor to be ceil-rounded. The shape and data type must be consistent with those of out. FLOAT16, FLOAT, DOUBLE, BFLOAT16 ND No more than 8 dimensions. √ out (aclTensor*) Output Output tensor that stores the result of the ceiling operation. The shape and data type must be the same as those of self. FLOAT16, FLOAT, DOUBLE, BFLOAT16 ND No more than 8 dimensions. √ workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the operator executor, which contains the operator computation process. - - - - - Atlas training products andAtlas inference products : BFLOAT16 data type is not supported.
Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The passed self or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self is not within the supported range. The data types of self and out are inconsistent. The shapes of self and out are inconsistent. The dimension of self exceeds 8.
aclnnCeil
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address applied for on the Device side. workspaceSize Input Workspace size applied for on the Device side, obtained from the first-phase API aclnnCeilGetWorkspaceSize. executor Input Operator executor, which contains the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
aclnnInplaceCeilGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data type. Data format. Dimensions (shape). Non-contiguous tensor. selfRef (aclTensor*) Input/Output Input/output tensor to be ceil-computed. The computation result is stored in selfRef. The shape and data type must be consistent with selfRef. FLOAT16, FLOAT, DOUBLE, BFLOAT16 ND No more than 8 dimensions. √ workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor(aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Atlas training products andAtlas inference products : BFLOAT16 data type is not supported.
Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The passed selfRef is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of selfRef is not within the supported range. The dimension of selfRef is greater than 8.
aclnnInplaceCeil
Parameters:
Parameter Input/Output Description workspace Input Address of the workspace memory applied for on the Device side. workspaceSize Input Size of the workspace applied for on the Device side, obtained through the first-phase API aclnnInplaceCeilGetWorkspaceSize. executor Input Operator executor, which contains the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnCeil&aclnnInplaceCeil defaults to a deterministic implementation.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_ceil.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
int64_t GetShapeSize(const std::vector<int64_t>& shape)
{
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
int Init(int32_t deviceId, aclrtStream* stream)
{
// Boilerplate: resource initialization.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return 0;
}
template <typename T>
int CreateAclTensor(
const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType,
aclTensor** tensor)
{
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to apply for device-side memory.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy data from the host side to the device-side memory.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Compute the strides of a contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(
shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(),
*deviceAddr);
return 0;
}
aclError InitAcl(int32_t deviceId, aclrtStream* stream)
{
auto ret = Init(deviceId, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
return ACL_SUCCESS;
}
aclError CreateInputs(
std::vector<int64_t>& inputShape, std::vector<int64_t>& outShape, void** selfDeviceAddr, void** outDeviceAddr,
aclTensor** self, aclTensor** out)
{
std::vector<double> selfHostData = {0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
std::vector<double> outHostData(8, 0);
// Create the self aclTensor.
auto ret = CreateAclTensor(selfHostData, inputShape, selfDeviceAddr, aclDataType::ACL_DOUBLE, self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, outDeviceAddr, aclDataType::ACL_DOUBLE, out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
return ACL_SUCCESS;
}
aclError ExecOpApi(
aclTensor* self, aclTensor* out, void** workspaceAddrOut, uint64_t& workspaceSize, void* selfDeviceAddr,
void* outDeviceAddr, std::vector<int64_t>& outShape, aclrtStream stream)
{
aclOpExecutor* executor;
auto size = GetShapeSize(outShape);
std::vector<double> resultData(size, 0);
// Example of calling the aclnnCeil API.
LOG_PRINT("test aclnnCeil\n");
// Call the first-phase API of aclnnCeil.
auto ret = aclnnCeilGetWorkspaceSize(self, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCeilGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for device memory based on workspaceSize.
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
*workspaceAddrOut = workspaceAddr;
// Call the second-phase API of aclnnCeil.
ret = aclnnCeil(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCeil failed. ERROR: %d\n", ret); return ret);
// Synchronize.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Copy the out result.
ret = aclrtMemcpy(
resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(resultData[0]),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %lf\n", i, resultData[i]);
}
// aclnnInplaceCeil call example
LOG_PRINT("\ntest aclnnInplaceCeil\n");
// Call the first-phase API of aclnnInplaceCeil.
ret = aclnnInplaceCeilGetWorkspaceSize(self, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceCeilGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for device memory again based on workspaceSize (same as the original logic).
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
*workspaceAddrOut = workspaceAddr;
// Call the second-phase API of aclnnInplaceCeil.
ret = aclnnInplaceCeil(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceCeil failed. ERROR: %d\n", ret); return ret);
// Synchronize.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Copy the self result.
ret = aclrtMemcpy(
resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, size * sizeof(resultData[0]),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
return ACL_SUCCESS;
}
int main()
{
// 1. Initialize the device/stream.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = InitAcl(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("InitAcl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output.
std::vector<int64_t> inputShape = {4, 2};
std::vector<int64_t> outShape = {4, 2};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
ret = CreateInputs(inputShape, outShape, &selfDeviceAddr, &outDeviceAddr, &self, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator APIs (ceil + inplace ceil).
uint64_t workspaceSize = 0;
void* workspaceAddr = nullptr;
ret = ExecOpApi(self, out, &workspaceAddr, workspaceSize, selfDeviceAddr, outDeviceAddr, outShape, stream);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 6. Release the aclTensor.
aclDestroyTensor(self);
aclDestroyTensor(out);
// 7. Release device resources.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}