# C++ integration - complete example needed

**URL:** https://discourse.julialang.org/t/c-integration-complete-example-needed/2214
**Category:** Numerics
**Created:** [February 21, 2017, 12:32pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214 "2017-02-21T12:32:04Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Albert65](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Albert65](https://discourse.julialang.org/u/Albert65)
#### Post date: [February 21, 2017, 12:32pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/1 "2017-02-21T12:32:04Z")

</div>

Hi All,

My application is currently written in Matlab with few very specific and time consuming functions implemented as a MEX extensions in C++. I am thinking about switching to Julia or Python, and here is the problem: how to integrate C++ code with the high-level language like Julia. I presume that a complete example of integration would be interesting for many researchers who need ultimate performance but lack of understanding of low level details.  
I would appreciate if somebody provided me with a sample C++ code that implements the following typical scenario:

1. Suppose an algorithm is implemented as a function within a shared or static library complied with Julia specific settings and headers (example?).
2. The function takes **dense** and **sparse** matrices as an input and returns the result in, say, a dense matrix.
3. The function should be able to get matrix sizes and reallocate the output matrix if necessary.
4. The function is supposed to be invoked many times and should be able to overwrite the elements of the external, persistent output matrix directly (once it has a correct size) rather than returning a temporary matrix, in order to void extra reallocations.  
The most interesting point is how to traverse the elements of Julia dense and sparse matrices.  
The sample code could implement a dense matrix times sparse matrix multiplication, for example.  
Say, KxM dense matrix times MxN sparse matrix assuming column major layout.  
Thank you in advance.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [February 21, 2017, 12:36pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/2 "2017-02-21T12:36:29Z")

</div>

Check out [GitHub - JuliaInterop/Cxx.jl: The Julia C++ Interface](https://github.com/Keno/Cxx.jl)

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 21, 2017, 12:37pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/3 "2017-02-21T12:37:12Z")

</div>

Can’t you just rewrite everything in Julia?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [February 21, 2017, 12:39pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/4 "2017-02-21T12:39:05Z")

</div>

(A bit off topic: checkout [https://github.com/JuliaInterop/MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl), which allows you to call matlab from Julia. So you can call your MEX files like this. Probably not the best performance wise but good for testing and a step-by-step migration.)

---

<div class="post-metadata">

### Author: ![Albert65](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Albert65](https://discourse.julialang.org/u/Albert65)
#### Post date: [February 21, 2017, 1:28pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/5 "2017-02-21T13:28:22Z")

</div>

There are some examples for DenseArray but nothing about sparse matrices.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [February 21, 2017, 1:35pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/6 "2017-02-21T13:35:01Z")

</div>

Are you asking the forum for help in working out how to do this yourself - or offering a salaried assignment with a set of predefined outcomes? I am sure there are quite many developers on the forum who would be interested in taking on a salaried assignment.

---

<div class="post-metadata">

### Author: ![Albert65](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Albert65](https://discourse.julialang.org/u/Albert65)
#### Post date: [February 21, 2017, 1:35pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/7 "2017-02-21T13:35:21Z")

</div>

I wish I could. We tried different vectorization and parallelization strategies using plain Matlab. However, due to specific nature of the algorithm, C++/Mex was far better solution.

---

<div class="post-metadata">

### Author: ![Albert65](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Albert65](https://discourse.julialang.org/u/Albert65)
#### Post date: [February 21, 2017, 1:37pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/8 "2017-02-21T13:37:59Z")

</div>

We need independence on platform x86 or Power. There is no Matlab for Power (at least we have no license), but MATLAB.jl relies on Matlab.

---

<div class="post-metadata">

### Author: ![Albert65](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Albert65](https://discourse.julialang.org/u/Albert65)
#### Post date: [February 21, 2017, 1:41pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/9 "2017-02-21T13:41:19Z")

</div>

Interesting suggestion, worth to keep in mind, thank you. Unfortunately, I cannot decide myself regarding any payment.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 21, 2017, 1:43pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/10 "2017-02-21T13:43:02Z")

</div>

That’s why rewriting in Julia is an excellent option. What technical requirements are preventing this?  
Is your code available? Otherwise it is basically impossible to provide assistance.

---

<div class="post-metadata">

### Author: ![Albert65](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Albert65](https://discourse.julialang.org/u/Albert65)
#### Post date: [February 21, 2017, 2:18pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/11 "2017-02-21T14:18:44Z")

</div>

Let me be more specific.

Input:  
dense matrices A, C : k x n  
sparse matrix B : (m\*n) x n

I need to compute the resultant m x k matrix:  
result = kron(eye(m), ones(1,n)) \* (repmat(transpose(A), m, 1) .\* (B \* transpose(C)))

The C++ code does not make repmat(…) explicitly but uses a modular arithmetic to  
expand the matrix A virtually. Otherwise, explicit construction of a huge  
(m\*n) x k matrix would be both time consuming (poor caching) and memory wasteful.  
Also, the C++ implementation does quite advance parallel job partition, which  
avoids large intermediate memory buffers and any inter-thread synchronization,  
besides a barrier at the very end.

This why I need direct access to (sparse) matrix elements.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [February 21, 2017, 2:20pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/12 "2017-02-21T14:20:43Z")

</div>

This should be trivial to implement in Julia at +/- the same speed as C++. Just try it and report back with a “Why is this slow…” post, usually people will be very helpful in getting it tuned.

---

<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: [February 21, 2017, 4:04pm UTC](https://discourse.julialang.org/t/c-integration-complete-example-needed/2214/13 "2017-02-21T16:04:19Z")

</div>

> [@Albert65](#):
>
> The C++ code does not make repmat(…) explicitly but uses a modular arithmetic toexpand the matrix A virtually. Otherwise, explicit construction of a huge (m\*n) x k matrix would be both time consuming (poor caching) and memory wasteful.Also, the C++ implementation does quite advance parallel job partition, whichavoids large intermediate memory buffers and any inter-thread synchronization,besides a barrier at the very end.

Julia is made to loop the same way C++ does. Copy the same algorithm over to Julia and you should be fine. It should be a pretty quick translation if there aren’t many library calls in there. It should end up with about the same speed (using similar compiler settings).

The interesting thing about Julia is that you can write things “like C/FORTRAN” but you don’t have to if you don’t want to, and if you do, it’ll compile to very similar (or the same) code as you’d get from those lower level languages.
