aclnnCeil&aclnnInplaceCeil
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Supported
Atlas training products : 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 a two-phase API. Call "aclnnCeilGetWorkspaceSize" or "aclnnInplaceCeilGetWorkspaceSize" first to obtain the workspace size required for computation and the executor containing the operator computation process. Then call "aclnnCeil" or "aclnnInplaceCeil" 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 Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input Input tensor to be rounded up. The shape and data type must be the same as those of out. FLOAT16, FLOAT, DOUBLE, BFLOAT16 ND No more than 8 dimensions β out (aclTensor*) Output Output tensor that stores the rounding-up result. 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 applied for on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - - Atlas training products andAtlas inference products : BFLOAT16 data type is not supported.
Return value
aclnnStatus: Return status code. For details, see also aclnn return code.
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 self or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self is not supported. The data types of self and out are inconsistent. The shapes of self and out are inconsistent. The number of dimensions of self is greater than 8.
aclnnCeil
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Workspace size allocated on the Device side, obtained by the first-phase API aclnnCeilGetWorkspaceSize. executor Input op executor, contains the operator computation flow. stream Input Specifies the Stream for executing tasks. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
aclnnInplaceCeilGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor selfRef (aclTensor*) Input/Output Input and output tensor. Tensor to be rounded up. The calculation result is stored in selfRef. shape and data type must be the same as those of selfRef. FLOAT16, FLOAT, DOUBLE, BFLOAT16 ND At most 8 dimensions β workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Atlas training products andAtlas inference products : BFLOAT16 data type is not supported.
Return value
aclnnStatus: return status code. See also 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 input selfRef is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of selfRef is not supported. The dimension of selfRef is greater than 8.
aclnnInplaceCeil
Parameters:
Parameter Input/Output Description workspace Input Pointer to the workspace memory allocated on the Device. workspaceSize Input Size of the workspace allocated on the Device, obtained by the first-phase API aclnnInplaceCeilGetWorkspaceSize. executor Input Executor of the op, containing the computation flow of the operator. stream Input Stream for task execution. Return value
aclnnStatus: Return code. See also aclnn return codes.
Constraints
- Deterministic computation:
- aclnnCeil&aclnnInplaceCeil defaults to a deterministic implementation.
Example
The following sample code is for reference only. For the compilation and execution procedure, 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 allocate 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 host-side data to 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);
// Calculate the strides of the 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);
// Allocate device memory according to 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);
// Allocate device memory again based on workspaceSize (same as the preceding 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. Device/stream initialization
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 inputs and outputs.
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;
}