Skip to content

Commit

Permalink
i32 and size_t memory variants, parallel data and control variants
Browse files Browse the repository at this point in the history
Also a rusty benchmark, oh, and no output until a --quiet flag is
made
  • Loading branch information
victorMoneratto committed Nov 1, 2016
1 parent d17afa2 commit 104141f
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 44 deletions.
109 changes: 109 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

# Created by https://www.gitignore.io/api/linux,windows,visualstudiocode,cmake,c

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json


### CMake ###
CMakeCache.txt
CMakeFiles
CMakeScripts
Makefile
cmake_install.cmake
install_manifest.txt
CTestTestfile.cmake


### C ###
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
modules.order
Module.symvers
Mkfile.old
dkms.conf
51 changes: 51 additions & 0 deletions benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

run() {
local N=4294967294
local I=1
# while [ $N -le $MAX ]; do
TIME=$(date +%s%3N)
(./sieve $N) #> /dev/null
if [ $? -gt 0 ]; then exit; fi
echo $I. $(($(date +%s%3N) - $TIME))ms looking until $N
I=$((I+1))
N=$(($N*10))
# done
}

compare() {
export OMP_NUM_THREADS=1
echo ********Running with OMP_NUM_THREADS = $OMP_NUM_THREADS********
run

echo ""

unset OMP_NUM_THREADS
echo ********Running with default OMP_NUM_THREADS********
run
}

MAX="$(echo "10^$1" | bc)"
IN=$1
COMPILER="gcc sieve.c -lm -fopenmp -Wall -Wextra -Werror -std=c99 -o sieve"

echo "############### SLOW32 ###############"
$COMPILER
compare
echo "######################################"

echo "############### SLOWEX ###############"
$COMPILER -DSIEVE_EXTENDED
compare
echo "######################################"

echo "############### FAST32 ###############"
$COMPILER -DSIEVE_FAST
compare
echo "######################################"

echo "############### FASTEX ###############"
$COMPILER -DSIEVE_FAST -DSIEVE_EXTENDED
compare
echo "######################################"

135 changes: 135 additions & 0 deletions sieve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <omp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

//
// NOTE: Type definitions
//
typedef uint8_t b8;
#define true 1
#define false 0

typedef uint32_t u32;
#define PrintU32 PRIu32
#define MAX_U32 UINT32_MAX

#ifdef SIEVE_EXTENDED
#define sieve_num size_t
#define PrintSieveNum "zu" // No ideal portable version on inttypes :(
#define MAX_SIEVE SIZE_MAX
#else
#define sieve_num u32
#define PrintSieveNum PrintU32
#define MAX_SIEVE MAX_U32
#endif

uintmax_t StringToUMax(char* Str, uintmax_t MaxVal) {
uintmax_t Val = strtoumax(Str, NULL, 10);
if (Val > MaxVal) {
Val = MaxVal;
errno = ERANGE;
}
//
// NOTE: In case Val > UINTMAX_MAX, errno is already set to ERANGE,
// there's no need for additional checks
//

return Val;
}
#define StringToSieveNum(Str) (sieve_num) StringToUMax(Str, MAX_SIEVE)

//
// NOTE: Scope based free (assuming gcc here)
//
void _Autofree(void* Ptr) {
void** _Ptr = Ptr;
if (*_Ptr)
free(*_Ptr);
}
#define AUTOFREE(Ptr) __attribute__((cleanup(_Autofree))) Ptr = NULL

void ShowUsage(char* Cmd) {
fprintf(stderr, "USAGE: %s <max>\n", Cmd);
}

int main(int NumberOfArgs, char** Args) {
//
// NOTE: Parsing arguments
//
if (NumberOfArgs != 2) {
ShowUsage(Args[0]);
return EXIT_FAILURE;
}

sieve_num Max = StringToSieveNum(Args[1]);
// if (errno == EINVAL) *((b8*)0) = 0;
if (errno == ERANGE || Max == MAX_SIEVE)
{
//
// NOTE: Passed value is over our limit (MAX_SIEVE-1)
// We will cap it and warn the user
//
Max = MAX_SIEVE - 1;
fprintf(stderr,
"%s is higher than we can handle, "
"looking to a max of %" PrintSieveNum " instead\n",
Args[1], Max);
}
// Biased float conversion seems safe until 64bits integer
sieve_num MaxSqrt = (sieve_num)(sqrtf((float)Max) + 1e-5f);

//
// NOTE: Sieve table allocation
//
sieve_num ElementsToAllocate = Max + 1;
b8* AUTOFREE(IsComposite);
IsComposite = (b8*)calloc((size_t)ElementsToAllocate, sizeof(b8));
if (IsComposite == 0 || errno == ENOMEM) {
fprintf(stderr,
"Problem allocating memory, try looking for lower maximum "
"values\n");
return EXIT_FAILURE;
}

//
// NOTE: Sieve algorithm
//
#ifndef SIEVE_FAST
#pragma omp parallel for shared(MaxSqrt) shared(IsComposite) schedule(static)
#endif
for (sieve_num Number = 2; Number <= MaxSqrt; Number++) {
if (!IsComposite[Number]) {
#ifdef SIEVE_FAST
#pragma omp parallel for firstprivate(Number) schedule(static)
#endif
for (sieve_num Composite = Number * Number; Composite <= Max;
Composite += Number) {
IsComposite[Composite] = true;
}
}
}
#if 0
//
// NOTE: Output
//
u32 PrimeInLine = 0;
const u32 PrimesPerLine = 12;
for (sieve_num Number = 2; Number <= Max; Number++) {
if (!IsComposite[Number]) {
PrimeInLine = (PrimeInLine + 1) % PrimesPerLine;
char* Separator = (PrimeInLine == 0) ? "\n" : ", ";
printf("%" PrintSieveNum "%s", Number, Separator);
}
}

// Append empty line if missing
if(PrimeInLine > 0) {
printf("\n");
}
#endif
return EXIT_SUCCESS;
}
44 changes: 0 additions & 44 deletions sieve_openmp.c

This file was deleted.

0 comments on commit 104141f

Please sign in to comment.