Skip to content

Commit 36967f4

Browse files
committed
Add more tests for C++ + OpenMP kernel
1 parent 3c2b9d0 commit 36967f4

1 file changed

Lines changed: 252 additions & 30 deletions

File tree

test/test_xcpp_kernel.py

Lines changed: 252 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -243,44 +243,266 @@ class BaseXCppTests2(jupyter_kernel_test.KernelTests):
243243
)
244244

245245
if platform.system() != 'Windows':
246+
246247
class BaseXCppOpenMPTests(jupyter_kernel_test.KernelTests):
247248
__test__ = False
248-
249-
# language_info.name in a kernel_info_reply should match this
250249
language_name = 'C++'
251250

252-
# OpenMP test that creates 2 threads, and gets them to output their thread
253-
# number in descending order
254-
code_omp="""
251+
# Common include block
252+
code_omp = """
255253
#include <omp.h>
256254
#include <iostream>
255+
#include <stdio.h>
257256
"""
258257

259-
code_omp_2="""
260-
int main() {
261-
omp_set_num_threads(2);
262-
#pragma omp parallel
263-
{
264-
if (omp_get_thread_num() == 1) {
265-
printf("1");
266-
#pragma omp barrier
267-
}
268-
else if (omp_get_thread_num() == 0) {
269-
#pragma omp barrier
270-
printf("0");
271-
}
272-
}
273-
}
274-
main();
275-
"""
276-
277-
def test_xcpp_omp(self):
278-
self.flush_channels()
279-
reply, output_msgs = self.execute_helper(code=self.code_omp,timeout=20)
280-
reply, output_msgs = self.execute_helper(code=self.code_omp_2,timeout=20)
281-
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
282-
self.assertEqual(output_msgs[0]['content']['text'], '10')
283-
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
258+
# ------------------------
259+
# Barrier ordering
260+
# ------------------------
261+
def test_barrier_order(self):
262+
code_omp_2 = """
263+
void main_barrier_order() {
264+
omp_set_num_threads(2);
265+
#pragma omp parallel
266+
{
267+
if (omp_get_thread_num() == 1) {
268+
printf("1");
269+
#pragma omp barrier
270+
} else {
271+
#pragma omp barrier
272+
printf("0");
273+
}
274+
}
275+
}
276+
main_barrier_order();
277+
"""
278+
self.flush_channels()
279+
self.execute_helper(code=self.code_omp, timeout=20)
280+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
281+
282+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
283+
self.assertEqual(output_msgs[0]['content']['text'], '10')
284+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
285+
286+
# ------------------------
287+
# Parallel for reduction
288+
# ------------------------
289+
def test_parallel_for_reduction(self):
290+
code_omp_2 = """
291+
void main_parallel_for_reduction() {
292+
int sum = 0;
293+
294+
#pragma omp parallel for reduction(+:sum)
295+
for (int i = 1; i <= 4; i++) {
296+
sum += i;
297+
}
298+
299+
std::cout << sum;
300+
}
301+
main_parallel_for_reduction();
302+
"""
303+
self.flush_channels()
304+
self.execute_helper(code=self.code_omp, timeout=20)
305+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
306+
307+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
308+
self.assertEqual(output_msgs[0]['content']['text'], '10')
309+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
310+
311+
# ------------------------
312+
# Critical
313+
# ------------------------
314+
def test_critical(self):
315+
code_omp_2 = """
316+
void main_critical() {
317+
int counter = 0;
318+
319+
#pragma omp parallel num_threads(4)
320+
{
321+
#pragma omp critical
322+
{
323+
counter++;
324+
}
325+
}
326+
327+
std::cout << counter;
328+
}
329+
main_critical();
330+
"""
331+
self.flush_channels()
332+
self.execute_helper(code=self.code_omp, timeout=20)
333+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
334+
335+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
336+
self.assertEqual(output_msgs[0]['content']['text'], '4')
337+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
338+
339+
# ------------------------
340+
# Atomic
341+
# ------------------------
342+
def test_atomic(self):
343+
code_omp_2 = """
344+
void main_atomic() {
345+
int counter = 0;
346+
347+
#pragma omp parallel num_threads(4)
348+
{
349+
#pragma omp atomic
350+
counter++;
351+
}
352+
353+
std::cout << counter;
354+
}
355+
main_atomic();
356+
"""
357+
self.flush_channels()
358+
self.execute_helper(code=self.code_omp, timeout=20)
359+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
360+
361+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
362+
self.assertEqual(output_msgs[0]['content']['text'], '4')
363+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
364+
365+
# ------------------------
366+
# Single
367+
# ------------------------
368+
def test_single(self):
369+
code_omp_2 = """
370+
void main_single() {
371+
int x = 0;
372+
373+
#pragma omp parallel num_threads(4)
374+
{
375+
#pragma omp single
376+
{
377+
x += 42;
378+
}
379+
}
380+
381+
std::cout << x;
382+
}
383+
main_single();
384+
"""
385+
self.flush_channels()
386+
self.execute_helper(code=self.code_omp, timeout=20)
387+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
388+
389+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
390+
self.assertEqual(output_msgs[0]['content']['text'], '42')
391+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
392+
393+
# ------------------------
394+
# Master
395+
# ------------------------
396+
def test_master(self):
397+
code_omp_2 = """
398+
void main_master() {
399+
int x = -1;
400+
401+
#pragma omp parallel num_threads(4)
402+
{
403+
#pragma omp master
404+
{
405+
x = omp_get_thread_num();
406+
}
407+
}
408+
409+
std::cout << x;
410+
}
411+
main_master();
412+
"""
413+
self.flush_channels()
414+
self.execute_helper(code=self.code_omp, timeout=20)
415+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
416+
417+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
418+
self.assertEqual(output_msgs[0]['content']['text'], '0')
419+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
420+
421+
# ------------------------
422+
# Task
423+
# ------------------------
424+
def test_task(self):
425+
code_omp_2 = """
426+
void main_task() {
427+
int x = 0;
428+
429+
#pragma omp parallel num_threads(2)
430+
{
431+
#pragma omp single
432+
{
433+
#pragma omp task
434+
{
435+
x = 5;
436+
}
437+
438+
#pragma omp taskwait
439+
}
440+
}
441+
442+
std::cout << x;
443+
}
444+
main_task();
445+
"""
446+
self.flush_channels()
447+
self.execute_helper(code=self.code_omp, timeout=20)
448+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
449+
450+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
451+
self.assertEqual(output_msgs[0]['content']['text'], '5')
452+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
453+
454+
# ------------------------
455+
# Ordered
456+
# ------------------------
457+
def test_ordered(self):
458+
code_omp_2 = """
459+
void main_ordered() {
460+
#pragma omp parallel for ordered num_threads(2)
461+
for (int i = 0; i < 4; i++) {
462+
#pragma omp ordered
463+
{
464+
printf("%d", i);
465+
}
466+
}
467+
}
468+
main_ordered();
469+
"""
470+
self.flush_channels()
471+
self.execute_helper(code=self.code_omp, timeout=20)
472+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
473+
474+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
475+
self.assertEqual(output_msgs[0]['content']['text'], '0123')
476+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
477+
478+
# ------------------------
479+
# Static schedule
480+
# ------------------------
481+
def test_schedule_static(self):
482+
code_omp_2 = """
483+
void main_schedule_static() {
484+
omp_set_num_threads(2);
485+
486+
int result[4];
487+
488+
#pragma omp parallel for schedule(static,1)
489+
for (int i = 0; i < 4; i++) {
490+
result[i] = omp_get_thread_num();
491+
}
492+
493+
for (int i = 0; i < 4; i++) {
494+
printf("%d", result[i]);
495+
}
496+
}
497+
main_schedule_static();
498+
"""
499+
self.flush_channels()
500+
self.execute_helper(code=self.code_omp, timeout=20)
501+
reply, output_msgs = self.execute_helper(code=code_omp_2, timeout=20)
502+
503+
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
504+
self.assertEqual(output_msgs[0]['content']['text'], '0101')
505+
self.assertEqual(output_msgs[0]['content']['name'], 'stdout')
284506

285507
kernel_names = ['xcpp23-omp']
286508

0 commit comments

Comments
 (0)