123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #include <starpu.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <assert.h>
- #include <sys/time.h>
- #include <common/blas.h>
- #ifdef STARPU_USE_CUDA
- #include <cublas.h>
- #endif
- #define TYPE float
- #define AXPY SAXPY
- #define CUBLASAXPY cublasSaxpy
- #define N (16*1024*1024)
- #define NBLOCKS 8
- #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
- TYPE *vec_x, *vec_y;
- starpu_data_handle_t handle_y, handle_x;
- void axpy_cpu(void *descr[], __attribute__((unused)) void *arg)
- {
- TYPE alpha = *((TYPE *)arg);
- unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
- TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
- TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
- AXPY((int)n, alpha, block_x, 1, block_y, 1);
- }
- #ifdef STARPU_USE_CUDA
- void axpy_gpu(void *descr[], __attribute__((unused)) void *arg)
- {
- TYPE alpha = *((TYPE *)arg);
- unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
- TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
- TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
- CUBLASAXPY((int)n, alpha, block_x, 1, block_y, 1);
- cudaThreadSynchronize();
- }
- #endif
- static struct starpu_codelet axpy_cl =
- {
- .where =
- #ifdef STARPU_USE_CUDA
- STARPU_CUDA|
- #endif
- STARPU_CPU,
- .cpu_funcs = {axpy_cpu, NULL},
- #ifdef STARPU_USE_CUDA
- .cuda_funcs = {axpy_gpu, NULL},
- #endif
- .nbuffers = 2,
- .modes = {STARPU_R, STARPU_RW}
- };
- int main(int argc, char **argv)
- {
-
- starpu_init(NULL);
- starpu_helper_cublas_init();
-
- starpu_malloc((void **)&vec_x, N*sizeof(TYPE));
- assert(vec_x);
- starpu_malloc((void **)&vec_y, N*sizeof(TYPE));
- assert(vec_y);
- unsigned i;
- for (i = 0; i < N; i++)
- {
- vec_x[i] = 1.0f;
- vec_y[i] = 4.0f;
- }
- FPRINTF(stderr, "BEFORE x[0] = %2.2f\n", vec_x[0]);
- FPRINTF(stderr, "BEFORE y[0] = %2.2f\n", vec_y[0]);
-
- starpu_vector_data_register(&handle_x, 0, (uintptr_t)vec_x, N, sizeof(TYPE));
- starpu_vector_data_register(&handle_y, 0, (uintptr_t)vec_y, N, sizeof(TYPE));
-
- struct starpu_data_filter block_filter =
- {
- .filter_func = starpu_block_filter_func_vector,
- .nchildren = NBLOCKS
- };
- starpu_data_partition(handle_x, &block_filter);
- starpu_data_partition(handle_y, &block_filter);
- TYPE alpha = 3.41;
- struct timeval start;
- struct timeval end;
- gettimeofday(&start, NULL);
- unsigned b;
- for (b = 0; b < NBLOCKS; b++)
- {
- struct starpu_task *task = starpu_task_create();
- task->cl = &axpy_cl;
- task->cl_arg = α
- task->handles[0] = starpu_data_get_sub_data(handle_x, 1, b);
- task->handles[1] = starpu_data_get_sub_data(handle_y, 1, b);
- starpu_task_submit(task);
- }
- starpu_task_wait_for_all();
- starpu_data_unpartition(handle_x, 0);
- starpu_data_unpartition(handle_y, 0);
- starpu_data_unregister(handle_x);
- starpu_data_unregister(handle_y);
- gettimeofday(&end, NULL);
- double timing = (double)((end.tv_sec - start.tv_sec)*1000000 +
- (end.tv_usec - start.tv_usec));
- FPRINTF(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(TYPE)/timing);
- FPRINTF(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", vec_y[0], alpha);
- starpu_free((void *)vec_x);
- starpu_free((void *)vec_y);
-
- starpu_shutdown();
- return 0;
- }
|