This tutorial demonstrates how to use me_eval() to process large datasets efficiently by breaking them into smaller chunks.
When working with very large arrays (millions or billions of elements), you may want to:
- Reduce memory usage - Process data in smaller pieces
- Enable streaming - Read from disk, process, and write back without loading everything into memory
- Improve cache efficiency - Smaller chunks fit better in CPU cache
Let's process a large dataset of temperature conversions (Celsius to Fahrenheit):
#include <stdio.h>
#include <stdlib.h>
#include "miniexpr.h"
#define TOTAL_SIZE 1000000 // 1 million elements
#define CHUNK_SIZE 10000 // Process 10k at a time
int main() {
// Allocate a large array of temperatures in Celsius
double *celsius = malloc(TOTAL_SIZE * sizeof(double));
double *fahrenheit = malloc(TOTAL_SIZE * sizeof(double));
if (!celsius || !fahrenheit) {
printf("Memory allocation failed\n");
return 1;
}
// Initialize with sample data
for (int i = 0; i < TOTAL_SIZE; i++) {
celsius[i] = -50.0 + (i * 0.0001); // Range from -50°C to 50°C
}
// Define variables (minimal form - name only, dtypes default to ME_AUTO)
me_variable vars[] = {{"c"}};
// Compile the expression once for chunked evaluation
// All variables will use ME_FLOAT64 since output dtype is specified
int error;
me_expr *expr = NULL;
if (me_compile("c * 9/5 + 32", vars, 1, ME_FLOAT64, &error, &expr) != ME_COMPILE_SUCCESS) { /* handle error */ }
// Process in chunks
printf("Processing %d elements in chunks of %d...\n",
TOTAL_SIZE, CHUNK_SIZE);
int num_chunks = (TOTAL_SIZE + CHUNK_SIZE - 1) / CHUNK_SIZE;
for (int chunk = 0; chunk < num_chunks; chunk++) {
int offset = chunk * CHUNK_SIZE;
int current_chunk_size = CHUNK_SIZE;
// Last chunk might be smaller
if (offset + current_chunk_size > TOTAL_SIZE) {
current_chunk_size = TOTAL_SIZE - offset;
}
// Pointers to current chunk
const void *vars_block[] = {&celsius[offset]};
void *output_chunk = &fahrenheit[offset];
// Evaluate this chunk
if (me_eval(expr, vars_block, 1, output_chunk, current_chunk_size, NULL) != ME_EVAL_SUCCESS) { /* handle error */ }
if ((chunk + 1) % 10 == 0) {
printf("Processed chunk %d/%d (%.1f%%)\n",
chunk + 1, num_chunks,
100.0 * (chunk + 1) / num_chunks);
}
}
printf("Done!\n\n");
// Verify a few results
printf("Sample results:\n");
for (int i = 0; i < 5; i++) {
printf("%.2f°C = %.2f°F\n", celsius[i], fahrenheit[i]);
}
// Clean up
me_free(expr);
free(celsius);
free(fahrenheit);
return 0;
}Processing 1000000 elements in chunks of 10000...
Processed chunk 10/100 (10.0%)
Processed chunk 20/100 (20.0%)
Processed chunk 30/100 (30.0%)
Processed chunk 40/100 (40.0%)
Processed chunk 50/100 (50.0%)
Processed chunk 60/100 (60.0%)
Processed chunk 70/100 (70.0%)
Processed chunk 80/100 (80.0%)
Processed chunk 90/100 (90.0%)
Processed chunk 100/100 (100.0%)
Done!
Sample results:
-50.00°C = -58.00°F
-49.99°C = -57.99°F
-49.99°C = -57.99°F
-49.99°C = -57.98°F
-49.99°C = -57.98°F
Here's an example that reads data from a file, processes it in chunks, and writes results:
#include <stdio.h>
#include <stdlib.h>
#include "miniexpr.h"
#define CHUNK_SIZE 1000
int main() {
FILE *input = fopen("input.dat", "rb");
FILE *output = fopen("output.dat", "wb");
if (!input || !output) {
printf("Failed to open files\n");
return 1;
}
// Allocate chunk buffers
double *x_chunk = malloc(CHUNK_SIZE * sizeof(double));
double *y_chunk = malloc(CHUNK_SIZE * sizeof(double));
double *result_chunk = malloc(CHUNK_SIZE * sizeof(double));
// Define variables (just the names - everything else optional)
me_variable vars[] = {{"x"}, {"y"}};
int error;
me_expr *expr = NULL;
if (me_compile("sqrt(x*x + y*y)", vars, 2, ME_FLOAT64, &error, &expr) != ME_COMPILE_SUCCESS) { /* handle error */ }
// Process file in chunks
size_t total_processed = 0;
size_t elements_read;
while (1) {
// Read chunk from file
elements_read = fread(x_chunk, sizeof(double), CHUNK_SIZE, input);
if (elements_read == 0) break;
fread(y_chunk, sizeof(double), elements_read, input);
// Process this chunk
const void *vars_block[] = {x_chunk, y_chunk};
if (me_eval(expr, vars_block, 2, result_chunk, elements_read, NULL) != ME_EVAL_SUCCESS) { /* handle error */ }
// Write results
fwrite(result_chunk, sizeof(double), elements_read, output);
total_processed += elements_read;
printf("Processed %zu elements\r", total_processed);
fflush(stdout);
}
printf("\nTotal processed: %zu elements\n", total_processed);
me_free(expr);
cleanup:
free(x_chunk);
free(y_chunk);
free(result_chunk);
fclose(input);
fclose(output);
return 0;
}Processing related but separate arrays:
#include <stdio.h>
#include <stdlib.h>
#include "miniexpr.h"
int main() {
const int TOTAL = 100000;
const int CHUNK = 5000;
// Separate arrays for different metrics
float *temperature = malloc(TOTAL * sizeof(float));
float *pressure = malloc(TOTAL * sizeof(float));
float *volume = malloc(TOTAL * sizeof(float));
float *moles = malloc(TOTAL * sizeof(float));
// Initialize with sample data
for (int i = 0; i < TOTAL; i++) {
pressure[i] = 101325.0f + i * 10.0f; // Pascals
volume[i] = 0.001f + i * 0.000001f; // m³
moles[i] = 1.0f; // mol
}
// Ideal gas law: T = (P * V) / (n * R)
// R = 8.314 J/(mol·K)
// Define variables (just the names - everything else optional)
me_variable vars[] = {{"P"}, {"V"}, {"n"}};
int error;
me_expr *expr = NULL;
if (me_compile("(P * V) / (n * 8.314)", vars, 3, ME_FLOAT32, &error, &expr) != ME_COMPILE_SUCCESS) { /* handle error */ }
// Process in chunks
for (int offset = 0; offset < TOTAL; offset += CHUNK) {
int size = (offset + CHUNK > TOTAL) ? (TOTAL - offset) : CHUNK;
const void *vars_block[] = {
&pressure[offset],
&volume[offset],
&moles[offset]
};
if (me_eval(expr, vars_block, 3, &temperature[offset], size, NULL) != ME_EVAL_SUCCESS) { /* handle error */ }
}
printf("Computed temperatures for %d samples\n", TOTAL);
printf("First result: T = %.2f K (%.2f°C)\n",
temperature[0], temperature[0] - 273.15);
me_free(expr);
free(temperature);
free(pressure);
free(volume);
free(moles);
return 0;
}When working with b2nd arrays (chunk/ block grids with edge padding), use the _nd APIs:
me_compile_nd(expr, vars, nvars, dtype, ndims, shape, chunkshape, blockshape, ...)me_eval_nd(expr, vars_block, nvars, out_block, block_nitems, nchunk, nblock, params)me_nd_valid_nitems(expr, nchunk, nblock, &valid)
Key points:
shape,chunkshape,blockshapeare C-order arrays (length =ndims).nchunkis the zero-based chunk index over the whole array (C-order);nblockis the block index inside that chunk (also C-order).- Callers pass padded block buffers (size =
prod(blockshape)elements).me_eval_ndcomputes only the valid elements and zero-fills the padded tail in the output. - For expressions whose overall result is a scalar (reductions like
sum(x)orsum(x) + 1),output_blockonly needs space for one item;me_eval_ndwrites a single element and does not zero any tail. - For best performance with padding,
me_eval_ndpacks valid elements, evaluates once, and scatters back; fully valid blocks still take a single fast path. Predicate reduction fast paths are limited toany/allcomparisons;sum(x == c)falls back to the pack path.
Minimal 2D example (padding on edges):
int64_t shape[2] = {5, 4};
int32_t chunkshape[2] = {3, 3};
int32_t blockshape[2] = {2, 2};
me_variable vars[] = {{"x", ME_FLOAT64}, {"y", ME_FLOAT64}};
me_expr *expr = NULL;
int err;
me_compile_nd("x + y", vars, 2, ME_FLOAT64, 2,
shape, chunkshape, blockshape, &err, &expr);
/* Block buffers are always padded to prod(blockshape) */
double x_block[4], y_block[4], out_block[4];
const void *ptrs[] = {x_block, y_block};
int64_t nchunk = 1; /* chunk (1,1) in C-order for this shape */
int64_t nblock = 0; /* first block inside that chunk */
int64_t valid = 0;
me_nd_valid_nitems(expr, nchunk, nblock, &valid); /* tells how many outputs are real */
me_eval_nd(expr, ptrs, 2, out_block, 4, nchunk, nblock, NULL);
/* out_block[valid..] is zeroed */See examples/11_nd_padding_example.c for a fuller walkthrough, and bench/benchmark_nd_padding to gauge performance with different padding patterns.
- Compile once - Create the expression once, then reuse it for all chunks
- Manage chunk boundaries - Handle the last chunk which might be smaller
- Use const void arrays* - Pass pointers to chunk starts via
vars_block - Update pointers - For each chunk, point to the correct offset in your arrays
- Thread-safe -
me_eval()is safe for parallel processing from multiple threads
- Memory efficient: Process datasets larger than RAM
- Cache friendly: Better CPU cache utilization with smaller chunks
- Progress tracking: Easy to report progress during long operations
- Flexible: Can pause, resume, or skip chunks as needed