/* StarPU --- Runtime system for heterogeneous multicore architectures. * * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria * * StarPU is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * StarPU is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU Lesser General Public License in COPYING.LGPL for more details. */ #include #include "../helper.h" /* * Test writing back the result into main memory as soon as it is available */ static unsigned var = 0; static starpu_data_handle_t handle; /* * Increment codelet */ #ifdef STARPU_USE_OPENCL /* dummy OpenCL implementation */ static void increment_opencl_kernel(void *descr[], void *cl_arg) { (void)cl_arg; STARPU_SKIP_IF_VALGRIND; cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]); unsigned h_token; cl_command_queue queue; starpu_opencl_get_current_queue(&queue); clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL); h_token++; clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL); } #endif #ifdef STARPU_USE_CUDA static void increment_cuda_kernel(void *descr[], void *cl_arg) { (void)cl_arg; STARPU_SKIP_IF_VALGRIND; unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]); unsigned host_token; /* This is a dummy technique of course */ cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream()); cudaStreamSynchronize(starpu_cuda_get_local_stream()); host_token++; cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream()); } #endif void increment_cpu_kernel(void *descr[], void *cl_arg) { (void)cl_arg; STARPU_SKIP_IF_VALGRIND; unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]); *tokenptr = *tokenptr + 1; } static struct starpu_codelet increment_cl = { #ifdef STARPU_USE_CUDA .cuda_funcs = {increment_cuda_kernel}, .cuda_flags = {STARPU_CUDA_ASYNC}, #endif #ifdef STARPU_USE_OPENCL .opencl_funcs = {increment_opencl_kernel}, .opencl_flags = {STARPU_OPENCL_ASYNC}, #endif .cpu_funcs = {increment_cpu_kernel}, .cpu_funcs_name = {"increment_cpu_kernel"}, .nbuffers = 1, .modes = {STARPU_RW} }; int main(void) { int ret; ret = starpu_init(NULL); if (ret == -ENODEV) return STARPU_TEST_SKIPPED; STARPU_CHECK_RETURN_VALUE(ret, "starpu_init"); starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(unsigned)); /* Copy the handle in main memory every time it is modified */ uint32_t wt_mask = (1<cl = &increment_cl; task->handles[0] = handle; ret = starpu_task_submit(task); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit"); } } starpu_data_unregister(handle); ret = EXIT_SUCCESS; if (var != ntasks*nloops) { ret = EXIT_FAILURE; FPRINTF(stderr, "VAR is %u should be %u\n", var, ntasks); } starpu_shutdown(); STARPU_RETURN(ret); enodev: starpu_data_unregister(handle); fprintf(stderr, "WARNING: No one can execute this task\n"); /* yes, we do not perform the computation but we did detect that no one * could perform the kernel, so this is not an error from StarPU */ starpu_shutdown(); return STARPU_TEST_SKIPPED; }