基于共享内存的位图

 基于共享内存的位图,项目打包下载

 1 /*
 2 * Copyright 1993-2010 NVIDIA Corporation.  All rights reserved.
 3 *
 4 * NVIDIA Corporation and its licensors retain all intellectual property and
 5 * proprietary rights in and to this software and related documentation.
 6 * Any use, reproduction, disclosure, or distribution of this software
 7 * and related documentation without an express license agreement from
 8 * NVIDIA Corporation is strictly prohibited.
 9 *
10 * Please refer to the applicable NVIDIA end user license agreement (EULA)
11 * associated with this source code for terms and conditions that govern
12 * your use of this NVIDIA software.
13 *
14 */
15 #include <GL\glut.h>
16 #include "cuda.h"
17 #include "cuda_runtime.h"
18 #include "device_launch_parameters.h"
19 #include "cuda.h"
20 #include "../common/book.h"
21 #include "../common/cpu_bitmap.h"
22 
23 
24 #define DIM 1024
25 #define PI 3.1415926535897932f
26 
27 __global__ void kernel(unsigned char *ptr) {
28     // map from threadIdx/BlockIdx to pixel position
29     int x = threadIdx.x + blockIdx.x * blockDim.x;
30     int y = threadIdx.y + blockIdx.y * blockDim.y;
31     int offset = x + y * blockDim.x * gridDim.x;
32 
33     __shared__ float    shared[16][16];
34 
35     // now calculate the value at that position
36     const float period = 128.0f;
37 
38     shared[threadIdx.x][threadIdx.y] =
39         255 * (sinf(x*2.0f*PI / period) + 1.0f) *
40         (sinf(y*2.0f*PI / period) + 1.0f) / 4.0f;
41 
42     // removing this syncthreads shows graphically what happens
43     // when it doesn't exist.  this is an example of why we need it.
44     __syncthreads();
45 
46     ptr[offset * 4 + 0] = 0;
47     ptr[offset * 4 + 1] = shared[15 - threadIdx.x][15 - threadIdx.y];
48     ptr[offset * 4 + 2] = 0;
49     ptr[offset * 4 + 3] = 255;
50 }
51 
52 // globals needed by the update routine
53 struct DataBlock {
54     unsigned char   *dev_bitmap;
55 };
56 
57 int main(void) {
58     DataBlock   data;
59     CPUBitmap bitmap(DIM, DIM, &data);
60     unsigned char    *dev_bitmap;
61 
62     HANDLE_ERROR(cudaMalloc((void**)&dev_bitmap,
63         bitmap.image_size()));
64     data.dev_bitmap = dev_bitmap;
65 
66     dim3    grids(DIM / 16, DIM / 16);
67     dim3    threads(16, 16);
68     kernel <<<grids, threads >>>(dev_bitmap);
69 
70     HANDLE_ERROR(cudaMemcpy(bitmap.get_ptr(), dev_bitmap,
71         bitmap.image_size(),
72         cudaMemcpyDeviceToHost));
73 
74     HANDLE_ERROR(cudaFree(dev_bitmap));
75 
76     bitmap.display_and_exit();
77 }

kernel函数中加粗标红的 __syncthreads()在去掉和加上时的效果图是不一样的。

取消时:

加上时:

 

这也是为什么加上同步的重要性。

 

抛砖引玉

这个

int offset = x + y * blockDim.x * gridDim.x;

 以及

1 ptr[offset * 4 + 0] = 0;
2 ptr[offset * 4 + 1] = shared[15 - threadIdx.x][15 - threadIdx.y];
3 ptr[offset * 4 + 2] = 0;
4 ptr[offset * 4 + 3] = 255;

如何理解?!

转载于:https://www.cnblogs.com/liangliangdetianxia/p/3986256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个基于内存的简单文件系统的实现,但是请注意,这只是一个示例,没有经过全面的测试和优化,不能用于实际生产环境。代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_FILENAME_LEN 20 // 文件名最大长度 #define MAX_FILE_NUM 10 // 最大文件数 #define BLOCK_SIZE 1024 // 块大小 // 文件块结构体 typedef struct Block { int index; // 块编号 char data[BLOCK_SIZE]; // 块数据 struct Block *next; // 下一个块指针 } Block; // 文件结构体 typedef struct File { char name[MAX_FILENAME_LEN]; // 文件名 int size; // 文件大小 Block *block; // 文件块指针 } File; // 文件系统结构体 typedef struct FileSystem { File files[MAX_FILE_NUM]; // 文件数组 int file_num; // 文件数 char *disk; // 虚拟磁盘指针 int disk_size; // 虚拟磁盘大小 int *bitmap; // 磁盘块位图指针 } FileSystem; // 初始化虚拟磁盘 void init_disk(FileSystem *fs) { fs->disk = (char *)malloc(fs->disk_size); memset(fs->disk, 0, fs->disk_size); } // 初始化位图 void init_bitmap(FileSystem *fs) { fs->bitmap = (int *)malloc(fs->disk_size / BLOCK_SIZE * sizeof(int)); memset(fs->bitmap, 0, fs->disk_size / BLOCK_SIZE * sizeof(int)); } // 查找空闲块 int find_free_block(FileSystem *fs) { int i; for (i = 0; i < fs->disk_size / BLOCK_SIZE; i++) { if (fs->bitmap[i] == 0) { fs->bitmap[i] = 1; return i; } } return -1; } // 释放块 void free_block(FileSystem *fs, Block *block) { int index = block->index; fs->bitmap[index] = 0; free(block); } // 创建文件 void create_file(FileSystem *fs, const char *name, int size) { if (fs->file_num >= MAX_FILE_NUM) { printf("File system is full.\n"); return; } if (size > fs->disk_size) { printf("File size is too large.\n"); return; } int i, j, k; for (i = 0; i < fs->file_num; i++) { if (strcmp(fs->files[i].name, name) == 0) { printf("File already exists.\n"); return; } } File file; strncpy(file.name, name, MAX_FILENAME_LEN); file.size = size; file.block = NULL; int block_num = size / BLOCK_SIZE + (size % BLOCK_SIZE > 0 ? 1 : 0); int *blocks = (int *)malloc(block_num * sizeof(int)); for (i = 0; i < block_num; i++) { int index = find_free_block(fs); if (index == -1) { printf("Disk space is full.\n"); for (j = 0; j < i; j++) { free_block(fs, file.block); } return; } blocks[i] = index; Block *block = (Block *)malloc(sizeof(Block)); block->index = index; for (k = 0; k < BLOCK_SIZE; k++) { block->data[k] = '\0'; } block->next = NULL; if (file.block == NULL) { file.block = block; } else { Block *p = file.block; while (p->next != NULL) { p = p->next; } p->next = block; } } for (i = 0; i < block_num; i++) { int index = blocks[i]; memcpy(fs->disk + index * BLOCK_SIZE, file.block[i].data, BLOCK_SIZE); } free(blocks); fs->files[fs->file_num++] = file; printf("File created successfully.\n"); } // 删除文件 void delete_file(FileSystem *fs, const char *name) { int i, j; for (i = 0; i < fs->file_num; i++) { if (strcmp(fs->files[i].name, name) == 0) { Block *p = fs->files[i].block; while (p != NULL) { free_block(fs, p); p = p->next; } for (j = i; j < fs->file_num - 1; j++) { fs->files[j] = fs->files[j + 1]; } fs->file_num--; printf("File deleted successfully.\n"); return; } } printf("File not found.\n"); } // 读文件 void read_file(FileSystem *fs, const char *name) { int i; for (i = 0; i < fs->file_num; i++) { if (strcmp(fs->files[i].name, name) == 0) { Block *p = fs->files[i].block; while (p != NULL) { printf("%s", p->data); p = p->next; } printf("\n"); return; } } printf("File not found.\n"); } // 写文件 void write_file(FileSystem *fs, const char *name, const char *data) { int i; for (i = 0; i < fs->file_num; i++) { if (strcmp(fs->files[i].name, name) == 0) { int size = strlen(data); if (size > fs->files[i].size) { printf("File size is too large.\n"); return; } int block_num = size / BLOCK_SIZE + (size % BLOCK_SIZE > 0 ? 1 : 0); int j, k; Block *p = fs->files[i].block; for (j = 0; j < block_num; j++) { if (p == NULL) { printf("File block error.\n"); return; } memcpy(p->data, data + j * BLOCK_SIZE, BLOCK_SIZE); p = p->next; } for (j = block_num; j < fs->files[i].size / BLOCK_SIZE; j++) { if (p == NULL) { printf("File block error.\n"); return; } free_block(fs, p); p = p->next; } printf("File written successfully.\n"); return; } } printf("File not found.\n"); } // 显示文件列表 void list_files(FileSystem *fs) { int i; for (i = 0; i < fs->file_num; i++) { printf("%s (%d bytes)\n", fs->files[i].name, fs->files[i].size); } } int main() { FileSystem fs; fs.disk_size = 1024 * 1024; // 1MB init_disk(&fs); init_bitmap(&fs); create_file(&fs, "test1.txt", 1024); write_file(&fs, "test1.txt", "Hello World!\n"); read_file(&fs, "test1.txt"); create_file(&fs, "test2.txt", 2048); write_file(&fs, "test2.txt", "This is a test.\n"); read_file(&fs, "test2.txt"); list_files(&fs); delete_file(&fs, "test1.txt"); delete_file(&fs, "test2.txt"); free(fs.disk); free(fs.bitmap); return 0; } ``` 这个简单的文件系统实现了创建文件、删除文件、读文件、写文件和显示文件列表等基本功能,使用了块链表来管理文件块,使用了位图来管理虚拟磁盘空闲块。当然,这个实现还有很多不足之处,比如没有处理文件重名、共享和安全控制,没有提供文件的移位和改名等功能,也没有提供良好的界面和转储功能。如果需要实现更完整、更复杂的文件系统,需要进一步研究和实践。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值