Skip to content

Commit ae0be39

Browse files
committed
acl_kernel_if_update_status: factor out querying of kernel invocation status
Signed-off-by: Peter Colberg <[email protected]>
1 parent d334c4c commit ae0be39

File tree

1 file changed

+130
-113
lines changed

1 file changed

+130
-113
lines changed

src/acl_kernel_if.cpp

Lines changed: 130 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,132 @@ void acl_kernel_if_launch_kernel(acl_kernel_if *kern,
12851285
activation_id);
12861286
}
12871287

1288+
// Queries status of pending kernel invocations. Returns the number of finished
1289+
// kernel invocations in `finish_counter`, or 0 if no invocations have finished.
1290+
static void acl_kernel_if_update_status_query(acl_kernel_if *kern,
1291+
const unsigned int accel_id,
1292+
const int activation_id,
1293+
unsigned int &finish_counter,
1294+
unsigned int &printf_size) {
1295+
// Default return value.
1296+
finish_counter = 0;
1297+
1298+
// Read the accelerator's status register
1299+
unsigned int csr = 0;
1300+
acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_CSR, &csr);
1301+
1302+
// Ignore non-status bits.
1303+
// Required by Option 3 wrappers which now have a version info in
1304+
// top 16 bits.
1305+
csr = ACL_KERNEL_READ_BIT_RANGE(csr, KERNEL_CSR_LAST_STATUS_BIT, 0);
1306+
1307+
// Check for updated status bits
1308+
if (0 == (csr & KERNEL_CSR_STATUS_BITS_MASK)) {
1309+
return;
1310+
}
1311+
1312+
// Clear the status bits that we read
1313+
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d reporting status %x.\n",
1314+
accel_id, csr);
1315+
1316+
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_DONE) == 1) {
1317+
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d is done.\n", accel_id);
1318+
}
1319+
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_STALLED) == 1) {
1320+
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d is stalled.\n", accel_id);
1321+
}
1322+
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_UNSTALL) == 1) {
1323+
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d is unstalled.\n",
1324+
accel_id);
1325+
}
1326+
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_PROFILE_TEMPORAL_STATUS) == 1) {
1327+
ACL_KERNEL_IF_DEBUG_MSG(
1328+
kern, ":: Accelerator %d ready for temporal profile readback.\n",
1329+
accel_id);
1330+
}
1331+
1332+
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_DONE) == 0 &&
1333+
ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_STALLED) == 0 &&
1334+
ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_PROFILE_TEMPORAL_STATUS) == 0) {
1335+
return;
1336+
}
1337+
1338+
// read the printf buffer size from the kernel cra, just after the
1339+
// kernel arguments
1340+
printf_size = 0;
1341+
if (kern->accel_num_printfs[accel_id] > 0) {
1342+
acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_PRINTF_BUFFER_SIZE,
1343+
&printf_size);
1344+
assert(printf_size <= ACL_PRINTF_BUFFER_TOTAL_SIZE);
1345+
ACL_KERNEL_IF_DEBUG_MSG(kern,
1346+
":: Accelerator %d printf buffer size is %d.\n",
1347+
accel_id, printf_size);
1348+
1349+
// kernel is stalled because the printf buffer is full
1350+
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_STALLED) == 1) {
1351+
// clear interrupt
1352+
unsigned int new_csr = 0;
1353+
acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_CSR, &new_csr);
1354+
ACL_KERNEL_CLEAR_BIT(new_csr, KERNEL_CSR_STALLED);
1355+
1356+
ACL_KERNEL_IF_DEBUG_MSG(kern,
1357+
":: Calling acl_process_printf_buffer_fn with "
1358+
"activation_id=%d and printf_size=%u.\n",
1359+
activation_id, printf_size);
1360+
// update status, which will dump the printf buffer, set
1361+
// debug_dump_printf = 0
1362+
acl_process_printf_buffer_fn(activation_id, (int)printf_size, 0);
1363+
1364+
ACL_KERNEL_IF_DEBUG_MSG(
1365+
kern, ":: Accelerator %d new csr is %x.\n", accel_id,
1366+
ACL_KERNEL_READ_BIT_RANGE(new_csr, KERNEL_CSR_LAST_STATUS_BIT, 0));
1367+
1368+
acl_kernel_cra_write(kern, accel_id, KERNEL_OFFSET_CSR, new_csr);
1369+
return;
1370+
}
1371+
}
1372+
1373+
// Start profile counter readback if profile interrupt and not done
1374+
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_PROFILE_TEMPORAL_STATUS) != 0 &&
1375+
ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_DONE) == 0) {
1376+
ACL_KERNEL_IF_DEBUG_MSG(
1377+
kern, ":: Issuing profile reset command:: Accelerator %d.\n", accel_id);
1378+
1379+
// Reset temporal profiling counter
1380+
unsigned int ctrl_val;
1381+
if (acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_CSR, &ctrl_val)) {
1382+
ACL_KERNEL_IF_DEBUG_MSG(
1383+
kern, ":: Got bad status reading CSR ctrl reg:: Accelerator %d.\n",
1384+
accel_id);
1385+
}
1386+
ACL_KERNEL_SET_BIT(ctrl_val, KERNEL_CSR_PROFILE_TEMPORAL_RESET);
1387+
if (acl_kernel_cra_write(kern, accel_id, KERNEL_OFFSET_CSR, ctrl_val)) {
1388+
ACL_KERNEL_IF_DEBUG_MSG(
1389+
kern, ":: Got bad status writing CSR ctrl reg:: Accelerator %d.\n",
1390+
accel_id);
1391+
}
1392+
1393+
if (activation_id < 0) {
1394+
// This is an autorun kernel
1395+
acl_process_autorun_profiler_scan_chain(kern->physical_device_id,
1396+
accel_id);
1397+
} else {
1398+
acl_kernel_profile_fn(activation_id);
1399+
}
1400+
return;
1401+
}
1402+
1403+
if (kern->csr_version == CSR_VERSION_ID_18_1) {
1404+
// Only expect single completion for older csr version
1405+
finish_counter = 1;
1406+
} else {
1407+
acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_FINISH_COUNTER,
1408+
&finish_counter);
1409+
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d has %d finishes.\n",
1410+
accel_id, finish_counter);
1411+
}
1412+
}
1413+
12881414
// Called when we receive a kernel status interrupt. Cycle through all of
12891415
// the running accelerators and check for updated status.
12901416
void acl_kernel_if_update_status(acl_kernel_if *kern) {
@@ -1325,128 +1451,19 @@ void acl_kernel_if_update_status(acl_kernel_if *kern) {
13251451
}
13261452
}
13271453

1328-
// Read the accelerator's status register
1329-
unsigned int csr = 0;
1330-
acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_CSR, &csr);
1331-
1332-
// Ignore non-status bits.
1333-
// Required by Option 3 wrappers which now have a version info in
1334-
// top 16 bits.
1335-
csr = ACL_KERNEL_READ_BIT_RANGE(csr, KERNEL_CSR_LAST_STATUS_BIT, 0);
1336-
1337-
// Check for updated status bits
1338-
if (0 == (csr & KERNEL_CSR_STATUS_BITS_MASK)) {
1339-
continue;
1340-
}
1341-
1342-
// Clear the status bits that we read
1343-
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d reporting status %x.\n",
1344-
accel_id, csr);
1345-
1346-
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_DONE) == 1) {
1347-
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d is done.\n", accel_id);
1348-
}
1349-
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_STALLED) == 1) {
1350-
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d is stalled.\n",
1351-
accel_id);
1352-
}
1353-
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_UNSTALL) == 1) {
1354-
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d is unstalled.\n",
1355-
accel_id);
1356-
}
1357-
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_PROFILE_TEMPORAL_STATUS) == 1) {
1358-
ACL_KERNEL_IF_DEBUG_MSG(
1359-
kern, ":: Accelerator %d ready for temporal profile readback.\n",
1360-
accel_id);
1361-
}
1362-
1363-
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_DONE) == 0 &&
1364-
ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_STALLED) == 0 &&
1365-
ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_PROFILE_TEMPORAL_STATUS) == 0) {
1366-
continue;
1367-
}
1368-
13691454
const int activation_id = kern->accel_job_ids[accel_id][next_queue_back];
13701455

1371-
// read the printf buffer size from the kernel cra, just after the
1372-
// kernel arguments
1456+
unsigned int finish_counter = 0;
13731457
unsigned int printf_size = 0;
1374-
if (kern->accel_num_printfs[accel_id] > 0) {
1375-
acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_PRINTF_BUFFER_SIZE,
1376-
&printf_size);
1377-
assert(printf_size <= ACL_PRINTF_BUFFER_TOTAL_SIZE);
1378-
ACL_KERNEL_IF_DEBUG_MSG(kern,
1379-
":: Accelerator %d printf buffer size is %d.\n",
1380-
accel_id, printf_size);
1381-
1382-
// kernel is stalled because the printf buffer is full
1383-
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_STALLED) == 1) {
1384-
// clear interrupt
1385-
unsigned int new_csr = 0;
1386-
acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_CSR, &new_csr);
1387-
ACL_KERNEL_CLEAR_BIT(new_csr, KERNEL_CSR_STALLED);
1388-
1389-
ACL_KERNEL_IF_DEBUG_MSG(kern,
1390-
":: Calling acl_process_printf_buffer_fn with "
1391-
"activation_id=%d and printf_size=%u.\n",
1392-
activation_id, printf_size);
1393-
// update status, which will dump the printf buffer, set
1394-
// debug_dump_printf = 0
1395-
acl_process_printf_buffer_fn(activation_id, (int)printf_size, 0);
1396-
1397-
ACL_KERNEL_IF_DEBUG_MSG(
1398-
kern, ":: Accelerator %d new csr is %x.\n", accel_id,
1399-
ACL_KERNEL_READ_BIT_RANGE(new_csr, KERNEL_CSR_LAST_STATUS_BIT, 0));
1458+
acl_kernel_if_update_status_query(kern, accel_id, activation_id,
1459+
finish_counter, printf_size);
14001460

1401-
acl_kernel_cra_write(kern, accel_id, KERNEL_OFFSET_CSR, new_csr);
1402-
continue;
1403-
}
1404-
}
1405-
1406-
// Start profile counter readback if profile interrupt and not done
1407-
if (ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_PROFILE_TEMPORAL_STATUS) != 0 &&
1408-
ACL_KERNEL_READ_BIT(csr, KERNEL_CSR_DONE) == 0) {
1409-
ACL_KERNEL_IF_DEBUG_MSG(
1410-
kern, ":: Issuing profile reset command:: Accelerator %d.\n",
1411-
accel_id);
1412-
1413-
// Reset temporal profiling counter
1414-
unsigned int ctrl_val;
1415-
if (acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_CSR, &ctrl_val)) {
1416-
ACL_KERNEL_IF_DEBUG_MSG(
1417-
kern, ":: Got bad status reading CSR ctrl reg:: Accelerator %d.\n",
1418-
accel_id);
1419-
}
1420-
ACL_KERNEL_SET_BIT(ctrl_val, KERNEL_CSR_PROFILE_TEMPORAL_RESET);
1421-
if (acl_kernel_cra_write(kern, accel_id, KERNEL_OFFSET_CSR, ctrl_val)) {
1422-
ACL_KERNEL_IF_DEBUG_MSG(
1423-
kern, ":: Got bad status writing CSR ctrl reg:: Accelerator %d.\n",
1424-
accel_id);
1425-
}
1426-
1427-
if (activation_id < 0) {
1428-
// This is an autorun kernel
1429-
acl_process_autorun_profiler_scan_chain(kern->physical_device_id,
1430-
accel_id);
1431-
} else {
1432-
acl_kernel_profile_fn(activation_id);
1433-
}
1461+
if (!(finish_counter > 0)) {
14341462
continue;
14351463
}
14361464

14371465
kern->last_kern_update = acl_kernel_if_get_time_us(kern);
14381466

1439-
unsigned int finish_counter = 0;
1440-
if (kern->csr_version == CSR_VERSION_ID_18_1) {
1441-
// Only expect single completion for older csr version
1442-
finish_counter = 1;
1443-
} else {
1444-
acl_kernel_cra_read(kern, accel_id, KERNEL_OFFSET_FINISH_COUNTER,
1445-
&finish_counter);
1446-
ACL_KERNEL_IF_DEBUG_MSG(kern, ":: Accelerator %d has %d finishes.\n",
1447-
accel_id, finish_counter);
1448-
}
1449-
14501467
for (unsigned int i = 0; i < finish_counter; i++) {
14511468
const int activation_id = kern->accel_job_ids[accel_id][next_queue_back];
14521469

0 commit comments

Comments
 (0)