-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
OptimizeLogic
72 lines (58 loc) · 1.91 KB
/
OptimizeLogic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <omp.h>
#define MAX_ITERATIONS 100
#define CHUNK_SIZE 4 // Number of iterations to process in each batch
void PMLL_LogicLoop_Optimized() {
char buffer[1024]; // Buffer to store messages
int buffer_index = 0;
#pragma omp parallel for schedule(dynamic) simd
for (int i = 0; i < MAX_ITERATIONS; i += CHUNK_SIZE) {
for (int j = 0; j < CHUNK_SIZE; ++j) {
if (i + j < MAX_ITERATIONS) {
#pragma omp critical
{
// Aggregate messages into the buffer
buffer_index += snprintf(buffer + buffer_index, sizeof(buffer) - buffer_index,
"Updating memory graph at iteration %d\n", i + j);
// Print buffer if nearly full
if (buffer_index > sizeof(buffer) - 50) {
printf("%s", buffer);
buffer_index = 0; // Reset buffer
}
}
}
}
}
// Print any remaining messages in the buffer
if (buffer_index > 0) {
printf("%s", buffer);
}
}
int main() {
printf("Optimized Logic Loop:\n");
PMLL_LogicLoop_Optimized();
return 0;
}
int main() {
PMLL_LogicLoop_Optimized();
return 0;
}
#include <cuda_runtime.h>
#include <stdio.h>
__global__ void PMLL_LogicLoop_GPU(int *counter) {
int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid < MAX_ITERATIONS) {
printf("Updating memory graph at iteration %d\n", tid);
}
}
int main() {
int *d_counter;
cudaMalloc((void **)&d_counter, sizeof(int));
cudaMemset(d_counter, 0, sizeof(int));
dim3 blockSize(256);
dim3 gridSize((MAX_ITERATIONS + blockSize.x - 1) / blockSize.x);
PMLL_LogicLoop_GPU<<<gridSize, blockSize>>>(d_counter);
cudaDeviceSynchronize();
cudaFree(d_counter);
return 0;
}