# A humble request/challenge

**URL:** <https://discourse.julialang.org/t/a-humble-request-challenge/5286>\
**Category:** Optimization (Mathematical)\
**Created:** [August 8, 2017, 7:00pm UTC](https://discourse.julialang.org/t/a-humble-request-challenge/5286 "2017-08-08T19:00:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jzakiya/32/6416_2.png) [@jzakiya](https://discourse.julialang.org/u/jzakiya)\
**Post date:** [August 8, 2017, 7:00pm UTC](https://discourse.julialang.org/t/a-humble-request-challenge/5286/1 "2017-08-08T19:00:15Z")

</div>

Hi

I primarly use Ruby for math/science problems/projects because it’s so easy to program in, and it allows me to think about how to solve problems without worrying about how to code it. I’ve also played around with Crystal (aka Ruby on steroids) but it’s still young, and doesn’t let me (currently) do what I want, or without a lot of hassles, and I’ve just recently started looking at Nim (less than a month at time of writing).

I heard about Julia sometime in 2016(?) but never had any time/reason to learn it. But now maybe I have incentive to do so.

I developed a prime sieve called the _Sieve of Zakiya (SoZ)_, and it’s companion the _Segmented Sieve of Zakiya (SSoZ)_. I wrote a paper, **The Segmented Sieve of Zakiya (SSoZ)** which describes its mathematical foundations and algorithm, and provide a working C++ implementation at the end of the paper (compiled, run, and verified), though I don’t consider myself a C++ programmer, just funcitonal enough in it. It’s a relatively short program.

Here’s a link to read and download the paper:

> **[The Segmented Sieve of Zakiya (SSoZ) | PDF | Prime Number | Central...](https://www.scribd.com/doc/228155369/The-Segmented-Sieve-of-Zakiya-SSoZ)**
>
> This paper describes an efficient and fast method to implement a Segmented Sieve of Zakiya (SSoZ). Its structure is based on two key design concepts: 1) a segment is a byte array consisting of B integral bytes of KB residues groups for a given Prime...

My humble request/challenge is for a/some skilled Julians (?) to translate the code into idiomatic Julia to demonstrate the best way to code the algorithm in it. Extra points if someone can do a true parallel version of the algorithm, which I attempted to do using OpemMP, but what I did didnt’ seem to make the code faster than the serial version (see paper).

I assume coding this the _Julia way_ would look different than the C++ code, and be quite different than a direct translation into Julia using the same (probably suboptimal) structure.

Ultimately, I’d like to publish the results of benchmarks in different languages doing the SSoZ in an updated paper.

If anyone would be willing to take up the challenge I’d be pleased to answer any questions the best I can.

Thanks in advance.

Jabari

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 8, 2017, 7:34pm UTC](https://discourse.julialang.org/t/a-humble-request-challenge/5286/2 "2017-08-08T19:34:52Z")

</div>

> [@jzakiya](#):
>
> I assume coding this the Julia way would look different than the C++ code, and be quite different than a direct translation into Julia using the same (probably suboptimal) structure.

Actually, writing code in Julia in a C/C++/Fortran style (just all looping) should get about the same performance as C/C++/Fortran. Of course, there probably ways you can do it to make the code both much more compact and also more generic (without losing performance of course), but if you just write an algorithm as straight loops on arrays of `Float64` you’ll be fine if you put it in a function and make sure it’s type-stable.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jzakiya/32/6416_2.png) [@jzakiya](https://discourse.julialang.org/u/jzakiya)\
**Post date:** [August 8, 2017, 8:14pm UTC](https://discourse.julialang.org/t/a-humble-request-challenge/5286/3 "2017-08-08T20:14:52Z")

</div>

I’m particualry interested in performing the alogiirthm using parallel processing, which I thought Julia was specifically designed to do. I assume the memory model used in the serial C++ version may have to change, but I don’t know enough about Julia to assess the details of performing parallel processing.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 8, 2017, 8:18pm UTC](https://discourse.julialang.org/t/a-humble-request-challenge/5286/4 "2017-08-08T20:18:17Z")

</div>

> [@jzakiya](#):
>
> I assume the memory model used in the serial C++ version may have to change

OpenMP-style code is well suited to parallelize with Julia’s multithreading. However, the issue that you noted is that the C++ parallel version didn’t do well, and so I don’t think you’ll get good parallel performance anywhere until you can sort out how to fundamentally parallelize the algorithm.

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [August 8, 2017, 9:38pm UTC](https://discourse.julialang.org/t/a-humble-request-challenge/5286/5 "2017-08-08T21:38:13Z")

</div>

A nice thing about Julia is that the simpler forms of parallelism are easy to try. Your code looks like it might be amenable. It is that it’s easy to throw an `@simd` on the front of a loop to see what it buys you in terms of SIMD-type parallelism. You can also try using threads with `@thread` around an outer loop.

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [August 8, 2017, 9:40pm UTC](https://discourse.julialang.org/t/a-humble-request-challenge/5286/6 "2017-08-08T21:40:46Z")

</div>

Some of your arrays are small enough, you could probably benefit from StaticArrays:

> **[GitHub - JuliaArrays/StaticArrays.jl: Statically sized arrays for Julia](https://github.com/JuliaArrays/StaticArrays.jl)**
>
> Statically sized arrays for Julia. Contribute to JuliaArrays/StaticArrays.jl development by creating an account on GitHub.
