Skip to content

Commit

Permalink
cpp changes. malloc removed (Xilinx#2883)
Browse files Browse the repository at this point in the history
* cpp changes. malloc removed

* removed extra header includes

* changes as per comments

* minor
  • Loading branch information
sarab96 authored Feb 25, 2020
1 parent 1e344ec commit d16c8fb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/xma/include/lib/xmaxclbin.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ typedef struct XmaXclbinInfo
uint32_t reserved[32];
} XmaXclbinInfo;

char *xma_xclbin_file_open(const char *xclbin_name);
std::vector<char> xma_xclbin_file_open(const std::string& xclbin_name);
int xma_xclbin_info_get(char *buffer, XmaXclbinInfo *info);
int xma_xclbin_map2ddr(uint64_t bit_map, int* ddr_bank);
#endif
51 changes: 3 additions & 48 deletions src/xma/src/xmaapi/xmahw_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,14 @@ bool hal_configure(XmaHwCfg *hwcfg, XmaXclbinParameter *devXclbins, int32_t num_
return false;
}
std::string xclbin = std::string(devXclbins[i].xclbin_name);
char *buffer = xma_xclbin_file_open(xclbin.c_str());
if (!buffer)
{
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Could not open xclbin file %s\n",
xclbin.c_str());
return false;
}
std::vector<char> xclbin_buffer = xma_xclbin_file_open(xclbin);
char *buffer = xclbin_buffer.data();

int32_t rc = xma_xclbin_info_get(buffer, &info);
if (rc != XMA_SUCCESS)
{
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Could not get info for xclbin file %s\n",
xclbin.c_str());
free(buffer);
return false;
}

Expand All @@ -217,7 +212,6 @@ bool hal_configure(XmaHwCfg *hwcfg, XmaXclbinParameter *devXclbins, int32_t num_
dev_tmp1.handle = xclOpen(dev_index, NULL, XCL_QUIET);
if (dev_tmp1.handle == NULL){
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Unable to open device id: %d\n", dev_index);
free(buffer);
return false;
}
dev_tmp1.dev_index = dev_index;
Expand All @@ -227,14 +221,12 @@ bool hal_configure(XmaHwCfg *hwcfg, XmaXclbinParameter *devXclbins, int32_t num_
if (rc != 0)
{
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "xclGetDeviceInfo2 failed for device id: %d, rc=%d\n", dev_index, rc);
free(buffer);
return false;
}

/* Always attempt download xclbin */
rc = load_xclbin_to_device(dev_tmp1.handle, buffer);
if (rc != 0) {
free(buffer);
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Could not download xclbin file %s to device %d\n",
xclbin.c_str(), dev_index);
return false;
Expand All @@ -243,14 +235,12 @@ bool hal_configure(XmaHwCfg *hwcfg, XmaXclbinParameter *devXclbins, int32_t num_
dev_tmp1.number_of_cus = info.number_of_kernels;
dev_tmp1.number_of_mem_banks = info.number_of_mem_banks;
if (dev_tmp1.number_of_cus > MAX_XILINX_KERNELS + MAX_XILINX_SOFT_KERNELS) {
free(buffer);
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Could not download xclbin file %s to device %d\n",
xclbin.c_str(), dev_index);
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "XMA & XRT supports max of %d CUs but xclbin has %d number of CUs\n", MAX_XILINX_KERNELS + MAX_XILINX_SOFT_KERNELS, dev_tmp1.number_of_cus);
return false;
}
if (dev_tmp1.number_of_mem_banks > MAX_DDR_MAP) {
free(buffer);
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "XMA supports max of only %d mem banks\n", MAX_DDR_MAP);
return false;
}
Expand Down Expand Up @@ -331,7 +321,6 @@ bool hal_configure(XmaHwCfg *hwcfg, XmaXclbinParameter *devXclbins, int32_t num_
/* Not to open context on all CUs
Will open during session_create
if (xclOpenContext(dev_tmp1.handle, info.uuid, d, true) != 0) {
free(buffer);
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Failed to open context to this CU\n");
return false;
}
Expand Down Expand Up @@ -388,40 +377,6 @@ bool hal_configure(XmaHwCfg *hwcfg, XmaXclbinParameter *devXclbins, int32_t num_

cu_mask = cu_mask << 1;
}
/*
int32_t num_execbo = 0;
if (dev_tmp1.number_of_cus > MIN_EXECBO_POOL_SIZE) {
num_execbo = dev_tmp1.number_of_cus;
} else {
num_execbo = MIN_EXECBO_POOL_SIZE;
}
dev_tmp1.kernel_execbos.reserve(num_execbo);
dev_tmp1.num_execbo_allocated = num_execbo;
for (int32_t d = 0; d < num_execbo; d++) {
uint32_t bo_handle;
int execBO_size = MAX_EXECBO_BUFF_SIZE;
//uint32_t execBO_flags = (1<<31);
char *bo_data;
bo_handle = xclAllocBO(dev_tmp1.handle,
execBO_size,
0,
XCL_BO_FLAGS_EXECBUF);
if (!bo_handle || bo_handle == mNullBO)
{
free(buffer);
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Unable to create bo for cu start\n");
return false;
}
bo_data = (char*)xclMapBO(dev_tmp1.handle, bo_handle, true);
memset((void*)bo_data, 0x0, execBO_size);
dev_tmp1.kernel_execbos.emplace_back(XmaHwExecBO{});
XmaHwExecBO& dev_execbo = dev_tmp1.kernel_execbos.back();
dev_execbo.handle = bo_handle;
dev_execbo.data = bo_data;
}
*/
free(buffer);

//Opening virtual CU context as some applications may use soft kernels only
if (xclOpenContext(dev_tmp1.handle, info.uuid, -1, true) != 0) {
Expand Down
48 changes: 27 additions & 21 deletions src/xma/src/xmaapi/xmaxclbin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
#include <stdio.h>
//#include <stdio.h>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdexcept>
//#include <string.h>
//#include <stdlib.h>
//#include <stdint.h>
#include "xclbin.h"
#include "app/xmaerror.h"
#include "app/xmalogger.h"
Expand All @@ -35,27 +36,32 @@ static int get_xclbin_iplayout(char *buffer, XmaXclbinInfo *xclbin_info);
static int get_xclbin_mem_topology(char *buffer, XmaXclbinInfo *xclbin_info);
static int get_xclbin_connectivity(char *buffer, XmaXclbinInfo *xclbin_info);

char *xma_xclbin_file_open(const char *xclbin_name)
std::vector<char> xma_xclbin_file_open(const std::string& xclbin_name)
{
xma_logmsg(XMA_INFO_LOG, XMAAPI_MOD, "Loading %s\n", xclbin_name);

std::ifstream file(xclbin_name, std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);

char *buffer = (char*)malloc(size);
if (buffer == NULL) {
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Could not allocate buffer for file %s\n", xclbin_name);
return NULL;
xma_logmsg(XMA_INFO_LOG, XMAAPI_MOD, "Loading %s\n", xclbin_name.c_str());

std::ifstream infile(xclbin_name, std::ios::binary | std::ios::ate);
std::streamsize xclbin_size = infile.tellg();
infile.seekg(0, std::ios::beg);

std::vector<char> xclbin_buffer;
try {
xclbin_buffer.reserve(xclbin_size);
} catch (const std::bad_alloc& ex) {
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Could not allocate buffer for file %s\n", xclbin_name.c_str());
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Buffer allocation error: %s\n", ex.what());
throw;
} catch (...) {
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Could not allocate buffer for xclbin file %s\n", xclbin_name.c_str());
throw;
}
if (!file.read(buffer, size))
{
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Could not read file %s\n", xclbin_name);
free(buffer);
buffer = NULL;
infile.read(xclbin_buffer.data(), xclbin_size);
if (infile.gcount() != xclbin_size) {
xma_logmsg(XMA_ERROR_LOG, XMAAPI_MOD, "Unable to read full xclbin file %s\n", xclbin_name.c_str());
throw std::runtime_error("Unable to read full xclbin file");
}

return buffer;
return xclbin_buffer;
}

static int32_t kernel_max_channel_id(const ip_data& ip, std::string kernel_channels)
Expand Down

0 comments on commit d16c8fb

Please sign in to comment.