[ home / overboard ] [ soy / qa / raid / r ] [ int / pol ] [ a / an / asp / biz / mtv / r9k / tech / v / sude / x ] [ q / news / chive / rules / pass / bans / status ] [ wiki ]

A banner for soyjak.party

/pol/ - International /Pol/itics & /Bant/er

Politics & countrywars
Catalog
Name
Email
Subject
Comment
Flag
File
Password (For file deletion.)

File: images - 2026-09-02T11263….jpeg 📥︎ (29.99 KB, 568x352) ImgOps

 3975175[Quote]

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_WORKERS 7
#define NUM_ELEMENTS 1000000

typedef struct
{
float *input;
float *output;
int start;
int count;
} Job;

void *spe_worker(void *arg)
{
Job *job = (Job *)arg;

float local_buffer[4096];

int processed = 0;

while (processed < job->count)
{
int chunk = job->count - processed;

if (chunk > 4096)
chunk = 4096;

for (int i = 0; i < chunk; i++)
local_buffer[i] = job->input[job->start + processed + i];

for (int i = 0; i < chunk; i++)
{
float x = local_buffer[i];
local_buffer[i] = x * x + 2.0f * x + 1.0f;
}

for (int i = 0; i < chunk; i++)
job->output[job->start + processed + i] = local_buffer[i];

processed += chunk;
}

return NULL;
}

int main(void)
{
float *input = malloc(sizeof(float) * NUM_ELEMENTS);
float *output = malloc(sizeof(float) * NUM_ELEMENTS);

if (!input || !output)
return 1;

for (int i = 0; i < NUM_ELEMENTS; i++)
input[i] = (float)i;

pthread_t spe[NUM_WORKERS];
Job jobs[NUM_WORKERS];

int elements_per_spe = NUM_ELEMENTS / NUM_WORKERS;

for (int i = 0; i < NUM_WORKERS; i++)
{
jobs[i].input = input;
jobs[i].output = output;
jobs[i].start = i * elements_per_spe;

jobs[i].count =
(i == NUM_WORKERS - 1)
? NUM_ELEMENTS - jobs[i].start
: elements_per_spe;

pthread_create(
&spe[i],
NULL,
spe_worker,
&jobs[i]
);
}

for (int i = 0; i < NUM_WORKERS; i++)
pthread_join(spe[i], NULL);

printf("Result[0] = %f\n", output[0]);
printf("Result[999999] = %f\n", output[999999]);

free(input);
free(output);

return 0;
}


[Return][Catalog][Go to top][Post a Reply]
Delete Post [ ]
[ home / overboard ] [ soy / qa / raid / r ] [ int / pol ] [ a / an / asp / biz / mtv / r9k / tech / v / sude / x ] [ q / news / chive / rules / pass / bans / status ] [ wiki ]