Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/collectives/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ add_custom_command(
)

set(TILEXR_COLLECTIVES_SOURCE_FILE
host/collective_backend.cpp
host/collective_launcher.cpp
host/collective_utils.cpp
host/collective_kernel.cpp
host/perf_trace_report.cpp
host/perf_trace_session.cpp
host/tilexr_collectives.cpp
host/collective_backend.h
${TILEXR_COLLECTIVES_KERNEL_EMBED_CPP}
)

Expand Down
113 changes: 113 additions & 0 deletions src/collectives/host/collective_backend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2024-2026 TileXR Project
* This file is a part of the CANN Open Software.
* Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "collective_backend.h"

#include "../../comm/ccu/tilexr_ccu_backend.h"
#include "../../comm/tilexr_comm.h"

namespace TileXRCollectives {
namespace Host {
namespace {

BackendTestState g_testState {};

int DispatchAiv(const CollectiveRequest &request)
{
(void)request;
return g_testState.enabled ? g_testState.aivReturn : TileXR::TILEXR_SUCCESS;
}

int DispatchUdma(const CollectiveRequest &request)
{
if (g_testState.enabled) {
if (!g_testState.udmaInitialized) {
return TileXR::TILEXR_ERROR_NOT_INITIALIZED;
}
return g_testState.udmaSupported ? g_testState.udmaReturn : TileXR::TILEXR_ERROR_NOT_SUPPORT;
}

auto *comm = static_cast<TileXR::TileXRComm *>(request.comm);
if (comm == nullptr || !comm->IsUdmaAvailableForCollectives()) {
return TileXR::TILEXR_ERROR_NOT_INITIALIZED;
}
return TileXR::TILEXR_ERROR_NOT_SUPPORT;
}

int DispatchCcu(const CollectiveRequest &request)
{
if (g_testState.enabled) {
if (!g_testState.ccuInitialized) {
return TileXR::TILEXR_ERROR_NOT_INITIALIZED;
}
return g_testState.ccuSupported ? g_testState.ccuReturn : TileXR::TILEXR_ERROR_NOT_SUPPORT;
}

auto *comm = static_cast<TileXR::TileXRComm *>(request.comm);
TileXR::TileXRCcuBackend *backend = comm->GetCcuBackendForCollectives();
if (backend == nullptr || !backend->Available()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In production this condition is always true for a forced CCU request: none of the normal TileXRComm initialization paths call InitCcuBackend; the only caller is EnableCcuBackendForTest. Consequently the newly public TILEXR_COLLECTIVE_BACKEND_CCU option always returns TILEXR_ERROR_NOT_INITIALIZED. Please initialize the backend in the normal communicator lifecycle or lazily before dispatch.

return TileXR::TILEXR_ERROR_NOT_INITIALIZED;
}
TileXR::TileXRCcuCollectiveRequest ccuRequest {};
ccuRequest.type = request.type;
ccuRequest.sendBuf = request.sendBuf;
ccuRequest.recvBuf = request.recvBuf;
ccuRequest.count = request.count;
ccuRequest.dataType = request.dataType;
ccuRequest.reduceOp = request.reduceOp;
ccuRequest.root = request.root;
ccuRequest.stream = request.stream;
TileXR::TileXRCcuCollectivePlan plan {};
const int ret = backend->PrepareCollective(ccuRequest, &plan);
if (ret != TileXR::TILEXR_SUCCESS) {
return ret;
}
return backend->SubmitCollective(plan, request.stream);
}

} // namespace

int DispatchCollective(const CollectiveRequest &request, TileXRCollectiveBackend backend)
{
if (request.comm == nullptr || request.sendBuf == nullptr || request.recvBuf == nullptr || request.count <= 0) {
return TileXR::TILEXR_ERROR_PARA_CHECK_FAIL;
}

switch (backend) {
case TILEXR_COLLECTIVE_BACKEND_AIV:
return DispatchAiv(request);
case TILEXR_COLLECTIVE_BACKEND_UDMA:
return DispatchUdma(request);
case TILEXR_COLLECTIVE_BACKEND_CCU:
return DispatchCcu(request);
case TILEXR_COLLECTIVE_BACKEND_AUTO:
default:
if (g_testState.enabled && g_testState.ccuInitialized && g_testState.ccuSupported) {
return DispatchCcu(request);
}
if (g_testState.enabled && g_testState.udmaInitialized && g_testState.udmaSupported) {
return DispatchUdma(request);
}
return DispatchAiv(request);
}
}

void SetBackendTestState(const BackendTestState &state)
{
g_testState = state;
g_testState.enabled = true;
}

void ResetBackendTestState()
{
g_testState = BackendTestState {};
}

} // namespace Host
} // namespace TileXRCollectives
52 changes: 52 additions & 0 deletions src/collectives/host/collective_backend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024-2026 TileXR Project
* This file is a part of the CANN Open Software.
* Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#ifndef TILEXR_COLLECTIVES_HOST_COLLECTIVE_BACKEND_H
#define TILEXR_COLLECTIVES_HOST_COLLECTIVE_BACKEND_H

#include <cstdint>

#include "acl/acl_base.h"
#include "tilexr_collectives.h"
#include "tilexr_types.h"

namespace TileXRCollectives {
namespace Host {

struct CollectiveRequest {
TileXR::TileXRType type = TileXR::TileXRType::ALL_GATHER;
void *sendBuf = nullptr;
void *recvBuf = nullptr;
int64_t count = 0;
TileXR::TileXRDataType dataType = TileXR::TILEXR_DATA_TYPE_RESERVED;
TileXR::TileXRReduceOp reduceOp = TileXR::TILEXR_REDUCE_RESERVED;
int root = 0;
TileXRCommPtr comm = nullptr;
aclrtStream stream = nullptr;
};

struct BackendTestState {
bool enabled = false;
bool udmaInitialized = false;
bool udmaSupported = false;
int udmaReturn = TileXR::TILEXR_ERROR_NOT_SUPPORT;
bool ccuInitialized = false;
bool ccuSupported = false;
int ccuReturn = TileXR::TILEXR_ERROR_NOT_SUPPORT;
int aivReturn = TileXR::TILEXR_SUCCESS;
};

int DispatchCollective(const CollectiveRequest &request, TileXRCollectiveBackend backend);
void SetBackendTestState(const BackendTestState &state);
void ResetBackendTestState();

} // namespace Host
} // namespace TileXRCollectives

#endif // TILEXR_COLLECTIVES_HOST_COLLECTIVE_BACKEND_H
Loading