c++枚举类型&&StarPU实现矩阵乘

1) 枚举类型

cpp 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<iostream>
#include <time.h>

enum Color {
	RED,				//RED=0
	GREEN,		//GREEN=1
	BLUE			//BLUE=2
};
int main()
{
	//定义数据类型为枚举类型Color的数组
	enum Color sky[3] = { RED,RED,RED};
	for (int i = 0; i < 3; i++) {
		printf("%d\n",sky[i]);
	}
}

2) StarPU实现矩阵乘

cpp 复制代码
#include <starpu.h>

#define N 100

// 定义在CPU上执行的任务
static void cpu_task(void *buffers[], void *cl_arg) {
    // 获取输入和输出矩阵
    float *A = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);
    float *B = (float *)STARPU_VECTOR_GET_PTR(buffers[1]);
    float *C = (float *)STARPU_VECTOR_GET_PTR(buffers[2]);

    // 执行矩阵乘法
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            C[i * N + j] = 0;
            for (int k = 0; k < N; k++) {
                C[i * N + j] += A[i * N + k] * B[k * N + j];
            }
        }
    }
}                        
// 定义在GPU上执行的任务
static void gpu_task(void *buffers[], void *cl_arg) {
    // 获取输入和输出矩阵
    float *A = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);
    float *B = (float *)STARPU_VECTOR_GET_PTR(buffers[1]);
    float *C = (float *)STARPU_VECTOR_GET_PTR(buffers[2]);

    // 使用CUDA进行矩阵乘法
    // 这里省略具体的CUDA代码
}

int main(int argc, char **argv) {
    // 初始化StarPU
    starpu_init(NULL);

    // 分配数据
    float *A = (float *)malloc(N * sizeof(float));
    float *B = (float *)malloc(N * sizeof(float));
    float *C = (float *)malloc(N * sizeof(float));

    // 创建句柄
    starpu_data_handle_t handle_A, handle_B, handle_C;
    starpu_vector_data_register(&handle_A, STARPU_MAIN_RAM, (uintptr_t)A, N, sizeof(float));
    starpu_vector_data_register(&handle_B, STARPU_MAIN_RAM, (uintptr_t)B, N, sizeof(float));
    starpu_vector_data_register(&handle_C, STARPU_MAIN_RAM, (uintptr_t)C, N, sizeof(float));

    // 定义任务代码
    struct starpu_codelet cl;
    starpu_codelet_init(&cl);
    cl.where = STARPU_CPU | STARPU_CUDA;
    cl.cpu_funcs[0] = cpu_task;
    cl.cuda_funcs[0] = gpu_task;
    cl.nbuffers = 3;
    cl.modes[0] = STARPU_R;
    cl.modes[1] = STARPU_R;
    cl.modes[2] = STARPU_W;

    // 创建任务
    struct starpu_task *task = starpu_task_create();
    task->cl = &cl;
    //向缓冲区中填充数据
    task->handles[0] = handle_A;
    task->handles[1] = handle_B;
    task->handles[2] = handle_C;

    // 提交任务
    starpu_task_submit(task);

    // 等待任务完成
    starpu_task_wait_for_all();

    // 释放句柄
    starpu_data_unregister(handle_A);
    starpu_data_unregister(handle_B);
    starpu_data_unregister(handle_C);

    // 清理
    free(A);
    free(B);
    free(C);
    starpu_shutdown();
    return 0;
}
相关推荐
程序猿_极客38 分钟前
【2025 年最新版】Java JDK 安装与环境配置教程(附图文超详细,Windows+macOS 通用)
java·开发语言·windows·macos·jdk
一个不知名程序员www4 小时前
算法学习入门 --- 哈希表和unordered_map、unordered_set(C++)
c++·算法
二哈喇子!4 小时前
BOM模型
开发语言·前端·javascript·bom
C++ 老炮儿的技术栈4 小时前
在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
c语言·c++·windows·git·vscode·visual studio
二哈喇子!4 小时前
空指针异常
开发语言
咚为4 小时前
Rust Print 终极指南:从底层原理到全场景实战
开发语言·后端·rust
%xiao Q4 小时前
GESP C++五级-202406
android·开发语言·c++
Psycho_MrZhang4 小时前
Neo4j Python SDK手册
开发语言·python·neo4j
Traced back4 小时前
# C# + SQL Server 实现自动清理功能的完整方案:按数量与按日期双模式
开发语言·c#
Sarvartha4 小时前
C++ STL 栈的便捷使用
c++·算法