FPC for high-performance computing lazarus / 21-General ---------------------------------------------------------------------- LV · 2025-05-17 22:54 In one thread, one of the forum guests claimed that FPC is very slow and unsuitable for high-performance computing. Many arguments were given, from the Big Bang concept, the campaigns of Alexander the Great, the theory of evolution, to the present day.  This is very interesting and exciting, but my modest experience of using FPC- Lazarus for high-performance simulations, where resource-intensive algebraic operations occupy a significant share, does not confirm this thesis. There were the source codes and the results of comparison with GFortran and C++ in single-threaded tests, which showed a difference in execution speed of 5-10%. The situation is about the same in multi-threaded tests, ~ 10%. Here is an example of comparison under equal conditions of algebraic matrix operations with a dimension of 5000 x 5000 (on the same hardware and the same program logic using Fortran, C++, and Free Pascal). Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ----------- --------------------------------------------------------------          |      GFortan       |          C++       |       FPC       |          |  (GCC 14.2.0 -O3)  |   (GCC 14.2.0 -O3) |   3.2.2 (-O3)   |---- ---------------------------------------------------------------------T ime,  s    |       14.1         |          14.1      |      15.1     |--------------------------------------------------------------------- ----  Source codes in the appendix. Could someone please share their experience using FPC for intensive computing? ---------------------------------------------------------------------- Martin_fr · 2025-05-18 00:04 First of all, I have no examples of my own, none were I have comparisons to other compilers. I have some code that needs to run fast. And in some cases, I had to "help" fpc. That is I had to give up readability, and e.g. introduce low level pointer stuff. But again, I can't tell how other compilers would have done, and if they would have done the needed optimizations without my sacrifice. (I was told gcc should do it, but well: hear say) Looking at your example: I think your code will have very limited gains from "classic" optimizations. That is, the speed is not determined by how optimal the assemble in the loop is. Your matrix calculation probably (I have not tested / just from review) have the CPU waiting for the RAM. The reading of    FBTransposed[j * MATRIX_SIZE + k] means you read bits of data at a distance of 5k. Or in other word: One cache miss follows the next. The CPU is spending a lot of time waiting, waiting for data to be retrieved from the RAM. IIRC (it is ages since I looked at that) there are some smart ways to access matrices by accessing small parts of them, and reduce the amount of cache misses. But making the code cache friendly is a matter of algorithm. No compiler will change your algorithm. So that needs to be done in the code. I haven't spent time to re-read it now, but IIRC the wikipedia has some details: https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm (read the cache paragraph) I wonder what the numbers are, if you optimise for cache usage. ---------------------------------------------------------------------- Fibonacci · 2025-05-18 01:52 ChaCha20, 1 GB Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FPC = 6671 ms (153,50 MB/s)C   = 2594 ms (394.76 MB/s) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses sysutils, strutils; type  chacha20state = record    state: array[0..15] of dword;    keystream: array[0..15] of dword;    position: dword;  end; procedure chacha20_set_counter(var state: chacha20state; counter: dword);begin  state.state[12] := counter;  state.position := 64;end; procedure chacha20_init(var state: chacha20state; key, nonce: string; counter: dword=0);const  magic = 'expand 32-byte k';begin  fillchar(state, sizeof(state), 0);  move(magic[1], state.state[0], 16);  if length(key) > 32 then setlength(key, 32);  if key <> '' then move(key[1], state.state[4], length(key));  chacha20_set_counter(state, counter);  if length(nonce) > 12 then setlength(nonce, 12);  if nonce <> '' then move(nonce[1], state.state[13], length(nonce));end; function rotl32(x, n: dword): dword; inline;begin  result := (x shl n) or (x shr (32-n));end; procedure chacha20_quarterround(p: pdword; a, b, c, d: byte); inline;begin  p[a] += p[b]; p[d] := rotl32(p[d] xor p[a], 16);  p[c] += p[d]; p[b] := rotl32(p[b] xor p[c], 12);  p[a] += p[b]; p[d] := rotl32(p[d] xor p[a], 8);  p[c] += p[d]; p[b] := rotl32(p[b] xor p[c], 7);end; procedure chacha20_next_block(var state: chacha20state);var  i: integer;begin  move(state.state, state.keystream, 64);  for i := 1 to 10 do begin    chacha20_quarterround(@state.keystream, 0, 4, 8, 12);    chacha20_quarterround(@state.keystream, 1, 5, 9, 13);    chacha20_quarterround(@state.keystream, 2, 6, 10, 14);    chacha20_quarterround(@state.keystream, 3, 7, 11, 15);    chacha20_quarterround(@state.keystream, 0, 5, 10, 15);    chacha20_quarterround(@state.keystream, 1, 6, 11, 12);    chacha20_quarterround(@state.keystream, 2, 7, 8, 13);    chacha20_quarterround(@state.keystream, 3, 4, 9, 14);  end;  for i := 0 to high(state.keystream) do state.keystream[i] += state.state[i];  state.state[12] += 1;  state.position := 0;end; procedure chacha20_xor(var state: chacha20state; data: pointer; len: dword);var  i: dword;begin  for i := 0 to len-1 do begin    if state.position >= 64 then chacha20_next_block(state);    pbyte(data+i)^ := pbyte(data+i)^ xor pbyte(@state.keystream[0]+state.position)^;    inc(state.position);  end;end; const  size = 1024*1024*1024; var  cc: chacha20state;  data: pointer;  start, end_: qword; begin  chacha20_init(cc, DupeString('a', 32), DupeString('b', 12));  data := getmem(size);  start := GetTickCount64;  chacha20_xor(cc, data, size);  end_ := GetTickCount64;  writeln(end_-start, ' ms');  writeln('verify = ', pint32(@cc.keystream)^);  writeln(Format('%.2f MB/s', [(size/(1024*1024))/((end_-start)/1000)]));  readln;end. Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #include #include #include #include  struct chacha20_context {  uint32_t keystream32[16];  size_t position;   uint8_t key[32];  uint8_t nonce[12];  uint64_t counter;   uint32_t state[16];}; static uint32_t rotl32(uint32_t x, int n) {  return (x << n) | (x >> (32 - n));} static uint32_t pack4(const uint8_t *a) {  uint32_t res = 0;  res |= (uint32_t)a[0] << 0 * 8;  res |= (uint32_t)a[1] << 1 * 8;  res |= (uint32_t)a[2] << 2 * 8;  res |= (uint32_t)a[3] << 3 * 8;  return res;} static void chacha20_init_block(struct chacha20_context *ctx, uint8_t key[], uint8_t nonce[]) {  memcpy(ctx->key, key, sizeof(ctx->key));  memcpy(ctx->nonce, nonce, sizeof(ctx->nonce));   const uint8_t *magic_constant = (uint8_t*)"expand 32-byte k";  ctx->state[0] = pack4(magic_constant + 0 * 4);  ctx->state[1] = pack4(magic_constant + 1 * 4);  ctx->state[2] = pack4(magic_constant + 2 * 4);  ctx->state[3] = pack4(magic_constant + 3 * 4);  ctx->state[4] = pack4(key + 0 * 4);  ctx->state[5] = pack4(key + 1 * 4);  ctx->state[6] = pack4(key + 2 * 4);  ctx->state[7] = pack4(key + 3 * 4);  ctx->state[8] = pack4(key + 4 * 4);  ctx->state[9] = pack4(key + 5 * 4);  ctx->state[10] = pack4(key + 6 * 4);  ctx->state[11] = pack4(key + 7 * 4);  ctx->state[12] = 0;  ctx->state[13] = pack4(nonce + 0 * 4);  ctx->state[14] = pack4(nonce + 1 * 4);  ctx->state[15] = pack4(nonce + 2 * 4);   memcpy(ctx->nonce, nonce, sizeof(ctx->nonce));} static void chacha20_block_set_counter(struct chacha20_context *ctx, uint64_t counter) {  ctx->state[12] = (uint32_t)counter;  ctx->state[13] = pack4(ctx->nonce + 0 * 4) + (uint32_t)(counter >> 32);} static void chacha20_block_next(struct chacha20_context *ctx) {  for (int i = 0; i < 16; i++) ctx->keystream32[i] = ctx->state[i]; #define CHACHA20_QUARTERROUND(x, a, b, c, d) \  x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 16); \  x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 12); \  x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 8); \  x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 7);   for (int i = 0; i < 10; i++) {    CHACHA20_QUARTERROUND(ctx->keystream32, 0, 4, 8, 12)    CHACHA20_QUARTERROUND(ctx->keystream32, 1, 5, 9, 13)    CHACHA20_QUARTERROUND(ctx->keystream32, 2, 6, 10, 14)    CHACHA20_QUARTERROUND(ctx->keystream32, 3, 7, 11, 15)    CHACHA20_QUARTERROUND(ctx->keystream32, 0, 5, 10, 15)    CHACHA20_QUARTERROUND(ctx->keystream32, 1, 6, 11, 12)    CHACHA20_QUARTERROUND(ctx->keystream32, 2, 7, 8, 13)    CHACHA20_QUARTERROUND(ctx->keystream32, 3, 4, 9, 14)  }   for (int i = 0; i < 16; i++) ctx->keystream32[i] += ctx->state[i];   uint32_t *counter = ctx->state + 12;  counter[0]++;  if (0 == counter[0]) {    counter[1]++;    assert(0 != counter[1]);  }} void chacha20_init_context(struct chacha20_context *ctx, uint8_t key[], uint8_t nonce[], uint64_t counter) {  memset(ctx, 0, sizeof(struct chacha20_context));  chacha20_init_block(ctx, key, nonce);  chacha20_block_set_counter(ctx, counter);  ctx->counter = counter;  ctx->position = 64;} void chacha20_xor(struct chacha20_context *ctx, uint8_t *bytes, size_t n_bytes) {  uint8_t *keystream8 = (uint8_t*)ctx->keystream32;  for (size_t i = 0; i < n_bytes; i++) {  if (ctx->position >= 64) {      chacha20_block_next(ctx);      ctx->position = 0;    }    bytes[i] ^= keystream8[ctx->position];    ctx->position++;  }} int main() {  const size_t SIZE = 1024 * 1024 * 1024;   uint8_t *data = (uint8_t*)malloc(SIZE);  if (!data) {    fprintf(stderr, "malloc failed\n");    return 1;  }   uint8_t key[32];  uint8_t nonce[12];  memset(key, 'a', 32);  memset(nonce, 'b', 12);   struct chacha20_context ctx;  chacha20_init_context(&ctx, key, nonce, 0);   DWORD64 start = GetTickCount64();  chacha20_xor(&ctx, data, SIZE);  DWORD64 end = GetTickCount64();   printf("%llu ms\n", end - start);  printf("verify = %lld\n", *((uint32_t*)ctx.keystream32));   double elapsed_sec = (end-start) / 1000.0;  double megabytes = (double)SIZE / (1024.0 * 1024.0);  double speed = megabytes / elapsed_sec;  printf("%.2f MB/s\n", speed);    free(data);  return 0;} ---------------------------------------------------------------------- dbannon · 2025-05-18 02:31 In a previous life I worked with people writing and using HPC code on some (very) big systems. And the general approach was (with Fortran or C++) to do most of the work in libraries such as BLAS or LAPACK (etc). These libraries are highly optimized and designed to be "general purpose". So, all we need, IMHO is pascal binding for those libraries, there already exists ones for, eg C, Fortran, Java, why not Pascal. There is already an MPI interface, https://wiki.freepascal.org/MPICH - I have no idea how good it is. I would strongly recommend concentrating on MPI rather than threaded code, HPC admins will thank you ! Davo ---------------------------------------------------------------------- domasz · 2025-05-18 05:37 Usually code written in Delphi/FPC is slower than C/C++ but if we compare with most popular languages today: JavaScript, PHP, Python and Java then modern Pascal compilers are high-performance. (Java is sometimes fast in benchmarks, but they exclude Java VM's launch time which isn't fair) So does it really matter if there are a few languages with faster compilers when pretty much all other languages are slower? ---------------------------------------------------------------------- Thaddy · 2025-05-18 06:03 Quote from: domasz on May 18, 2025, 07:37:42 amUsually code written in Delphi/FPC is slower than C/C++ You don't get it do you? Like most of the other idiots that "compare" speed. It is ff'ing not the language, it is the compiler optimization and a lot of C++ compilers are slower than fpc. If you do still not understand that, please clean your mouth. Such statements are silly naive. ---------------------------------------------------------------------- paule32 · 2025-05-18 06:36 such comparsions a indeed, silly. each of us people there use other hardware. and each hardware have cons and pros. so, I thinking that only prophlets call for paper. ---------------------------------------------------------------------- domasz · 2025-05-18 06:41 Of course it's compiler/interpreter optimizations but C is a simple wrapper around Assembly while Pascal is more advanced. And this makes it harder to make an optimized compiler. Also there are huge corporations using/developing C so more people work on optimizations. ---------------------------------------------------------------------- jwdietrich · 2025-05-18 06:54 The speed of generated code depends on many factors. For a realistic use-case scenario involving object-oriented programming, we could demonstrate that Free Pascal generates faster code than Swift and C++ (and, of course, it was way faster than R and Python). Paper on implementation study ---------------------------------------------------------------------- schuler · 2025-05-18 07:29 @jwdietrich, thank you for the scientific evidence! ---------------------------------------------------------------------- munair · 2025-05-18 09:13 As I'm working on the SharpBASIC compiler (which despite the name is close to Pascal), optimization is a real challenge. C is indeed more of a wrapper around assembly, but not a "simple wrapper". A major impact on performance is type safety, range checking and error handling. C is minimal and difficult to debug as a result. So there need to be trade-offs with language construction and compiler behavior: easier to debug and safer for programmers, or the fastest / most optimized code possible. ---------------------------------------------------------------------- d2010 · 2025-05-18 13:50 Quote from: LV on May 18, 2025, 12:54:05 amIn one thread, one of the forum guests claimed that FPC is very slow and unsuitable for high- performance computing. Many arguments were He use dynamic-memory ,  but inside FPC,  Delphi, or even TurboPascal, --memory speed of read/write/scan/checking are more slow bellow the C+ or C++. In real facts C++ and GnuC, VC98,Vc2010  are more worst at crash- memory-leaks. The runtime FPC.exe (eg.system.ppu classes.ppu)   protect the all- sources.pp more  better  that "C++ protect the sources.cpp  or source.c or source.lib again memory-leaks". Anyway , everyone have experienced for C:Q1? C:Q1=How to "Visual C++ 2017" the sources.cpp  more better  that DelphiXe protect *.dpr? ---------------------------------------------------------------------- LV · 2025-05-18 14:14 Thanks to everyone for the helpful tips, comments, and suggestions. I appreciate them. As for ChaCha20, 1 GB, it's not my "kind of sport". However, I ran these codes on my computer (difference 313%): Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FPC 3.2.2  9578 msverify = 17398848106.91 MB/s C (GCC) 3063 msverify = 17398848334.31 MB/s  If we help the compiler a little and unroll the loops in both programs, the results will be as follows (difference 34%). Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FPC 3.2.2 Unrolled  2750 msverify = 17398848372.36 MB/s C (GCC) Unrolled 2047 msVerify = 17398848500.24 MB/s ---------------------------------------------------------------------- Martin_fr · 2025-05-18 14:48 The chacha20 seems to be significantly better in fpc trunk Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 322 o39375 ms verify = 17398848 109.23 MB/s9359 ms verify = 17398848 109.41 MB/s322 o49329 ms verify = 17398848 109.77 MB/s9344 ms verify = 17398848 109.59 MB/s 323 o39281 ms verify = 17398848 110.33 MB/s9281 ms verify = 17398848 110.33 MB/s 331 o35640 ms verify = 17398848 181.56 MB/s5672 ms verify = 17398848 180.54 MB/s331 o45656 ms verify = 17398848 181.05 MB/s ---------------------------------------------------------------------- LV · 2025-05-18 15:00 Quote from: Martin_fr on May 18, 2025, 04:48:04 pmThe chacha20 seems to be significantly better in fpc trunk Thanks, cool ---------------------------------------------------------------------- LV · 2025-05-18 15:27 @Martin_fr, could you run chacha20's code with loop unrolling using the fpc trunk compiler? Thanks. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; uses sysutils, strutils; type  chacha20state = record    state: array[0..15] of dword;    keystream: array[0..15] of dword;    position: dword;  end; procedure chacha20_set_counter(var state: chacha20state; counter: dword);begin  state.state[12] := counter;  state.position := 64;end; procedure chacha20_init(var state: chacha20state; key, nonce: string; counter: dword=0);const  magic = 'expand 32-byte k';begin  fillchar(state, sizeof(state), 0);  move(magic[1], state.state[0], 16);  if length(key) > 32 then setlength(key, 32);  if key <> '' then move(key[1], state.state[4], length(key));  chacha20_set_counter(state, counter);  if length(nonce) > 12 then setlength(nonce, 12);  if nonce <> '' then move(nonce[1], state.state[13], length(nonce));end; function rotl32(x, n: dword): dword; inline;begin  result := (x shl n) or (x shr (32-n));end; procedure chacha20_quarterround(p: pdword; a, b, c, d: integer); inline;var  a_val, b_val, c_val, d_val: dword;begin  a_val := p[a];  b_val := p[b];  c_val := p[c];  d_val := p[d];   a_val += b_val;  d_val := rotl32(d_val xor a_val, 16);  c_val += d_val;  b_val := rotl32(b_val xor c_val, 12);  a_val += b_val;  d_val := rotl32(d_val xor a_val, 8);  c_val += d_val;  b_val := rotl32(b_val xor c_val, 7);   p[a] := a_val;  p[b] := b_val;  p[c] := c_val;  p[d] := d_val;end; procedure chacha20_next_block(var state: chacha20state);var  i: integer;begin  move(state.state, state.keystream, 64);  for i := 1 to 10 do begin    // Column rounds  chacha20_quarterround(@state.keystream, 0, 4, 8, 12);    chacha20_quarterround(@state.keystream, 1, 5, 9, 13);    chacha20_quarterround(@state.keystream, 2, 6, 10, 14);    chacha20_quarterround(@state.keystream, 3, 7, 11, 15);    // Diagonal rounds    chacha20_quarterround(@state.keystream, 0, 5, 10, 15);    chacha20_quarterround(@state.keystream, 1, 6, 11, 12);    chacha20_quarterround(@state.keystream, 2, 7, 8, 13);    chacha20_quarterround(@state.keystream, 3, 4, 9, 14);  end;  for i := 0 to high(state.keystream) do    state.keystream[i] += state.state[i];  state.state[12] += 1;  state.position := 0;end; procedure chacha20_xor(var state: chacha20state; data: pointer; len: dword);var  p: PByte;  remain, block_count, i: dword;  pData, pKey: PDWord;begin  p := data;  remain := 64 - state.position;   if remain > 0 then begin    if remain >= 4 then begin      pData := PDWord(p);      pKey := @state.keystream[state.position div 4];      for i := 0 to (remain div 4) - 1 do begin        pData[i] := pData[i] xor pKey[i];      end;      inc(p, (remain div 4) * 4);      dec(len, (remain div 4) * 4);      inc(state.position, (remain div 4) * 4);    remain := remain mod 4;    end;     if remain > 0 then begin      if remain > len then remain := len;      for i := 0 to remain - 1 do      p[i] := p[i] xor pbyte(@state.keystream[state.position + i])^;      inc(p, remain);      dec(len, remain);      inc(state.position, remain);    end;  end;   block_count := len div 64;  if block_count > 0 then begin    pData := PDWord(p);    for i := 0 to block_count - 1 do begin      if state.position >= 64 then        chacha20_next_block(state);      pKey := @state.keystream[0];       pData[0] := pData[0] xor pKey[0];      pData[1] := pData[1] xor pKey[1];      pData[2] := pData[2] xor pKey[2];      pData[3] := pData[3] xor pKey[3];      pData[4] := pData[4] xor pKey[4];      pData[5] := pData[5] xor pKey[5];      pData[6] := pData[6] xor pKey[6];      pData[7] := pData[7] xor pKey[7];      pData[8] := pData[8] xor pKey[8];      pData[9] := pData[9] xor pKey[9];      pData[10] := pData[10] xor pKey[10];      pData[11] := pData[11] xor pKey[11];      pData[12] := pData[12] xor pKey[12];      pData[13] := pData[13] xor pKey[13];      pData[14] := pData[14] xor pKey[14];    pData[15] := pData[15] xor pKey[15];       inc(pData, 16);      state.position := 64;    end;    len := len mod 64;    p := PByte(pData);  end;   if len > 0 then begin    if state.position >= 64 then      chacha20_next_block(state);    if len >= 4 then begin      pData := PDWord(p);      pKey := @state.keystream[state.position div 4];      for i := 0 to (len div 4) - 1 do begin        pData[i] := pData[i] xor pKey[i];      end;      inc(p, (len div 4) * 4);      dec(len, (len div 4) * 4);      inc(state.position, (len div 4) * 4);    end;    for i := 0 to len - 1 do      p[i] := p[i] xor pbyte(@state.keystream[state.position + i])^;    inc(state.position, len);  end;end; const  size = 1024*1024*1024; var  cc: chacha20state;  data: pointer;  start, end_: qword; begin  chacha20_init(cc, DupeString('a', 32), DupeString('b', 12));  data := getmem(size);  start := GetTickCount64;  chacha20_xor(cc, data, size);  end_ := GetTickCount64;  writeln('FPC 3.2.2 Unrolled  ',end_-start, ' ms');  writeln('verify = ', pint32(@cc.keystream)^);  writeln(Format('%.2f MB/s', [(size/(1024*1024))/((end_-start)/1000)]));  readln;end. ---------------------------------------------------------------------- Martin_fr · 2025-05-18 15:54 Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FPC 3.2.2 Unrolled  2578 msverify = 17398848397.21 MB/s I tested his code with trunk / O3 and {$Optimization noLOOPUNROLL} to make sure trunk hadn't simply done more unroll already. But that made no difference. Btw Core I7-8700K I was actually surprised of the huge improvement. I know trunk had some work done. But in other tests, the gains were around 4% to 5%. From what I have seen (commits / mail list) a lot of work went into the peephole optimizer. That are usually local optimizations (register usages over short bits of code, avoid storing to mem, change 2 statements to avoid wait cycles...) I don't know if there was any work on other optimization parts such as DFA and the overall register allocation. ---------------------------------------------------------------------- LV · 2025-05-18 16:00 Thank you very much, I am delighted with the work of the FPC-Lazarus team. ---------------------------------------------------------------------- Jorg3000 · 2025-05-18 16:18 Is there a speed change with the following code change? *EDIT*  Oops, this is based on the code from page 1 and may already be outdated. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure chacha20_xor(var state: chacha20state; data: pointer; len: dword);var  i: dword;  p: PByte;  pKey0: Pointer;begin  p := data;  pKey0 := @state.keystream[0];   for i := 0 to len-1 do begin    if state.position >= 64 then chacha20_next_block(state);    p^ := p^ xor pbyte(pKey0 + state.position)^;    inc(p);    inc(state.position);  end;end; ---------------------------------------------------------------------- ALLIGATOR · 2025-05-18 17:32 I commented out that section of code completely and the Verify value did not change. How can I verify that the algorithm is working correctly? https://forum.lazarus.freepascal.org/index.php/topic,71174.msg555295.h tml#msg555295 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       //pData[0] := pData[0] xor pKey[0];      //pData[1] := pData[1] xor pKey[1];      //pData[2] := pData[2] xor pKey[2];      //pData[3] := pData[3] xor pKey[3];      //pData[4] := pData[4] xor pKey[4];      //pData[5] := pData[5] xor pKey[5];      //pData[6] := pData[6] xor pKey[6];      //pData[7] := pData[7] xor pKey[7];      //pData[8] := pData[8] xor pKey[8];      //pData[9] := pData[9] xor pKey[9];      //pData[10] := pData[10] xor pKey[10];      //pData[11] := pData[11] xor pKey[11];      //pData[12] := pData[12] xor pKey[12];      //pData[13] := pData[13] xor pKey[13];      //pData[14] := pData[14] xor pKey[14];      //pData[15] := pData[15] xor pKey[15]; ---------------------------------------------------------------------- Fibonacci · 2025-05-18 17:55 You commented out data encryption (xoring), while the verify value is the first 4 bytes of the last 64-byte keystream block. But I checked the data and it is correct, same output as libsodium. Here with data verification, the last 4 bytes: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  chacha20_init(cc, DupeString('a', 32), DupeString('b', 12));  data := getmem(size);  fillchar(data^, size, 'x');  start := GetTickCount64;  chacha20_xor(cc, data, size);  end_ := GetTickCount64;  writeln(end_- start, ' ms');  writeln('verify = ', pint32(@cc.keystream)^);  writeln('verify2 = ', pint32(data+size-4)^);  writeln(Format('%.2f MB/s', [(size/(1024*1024))/((end_-start)/1000)]));  readln;end. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FPC 3.3.1 unrolled: 2765 ms verify2 = -643586287  370,34 MB/sFPC+libsodium:     547 ms verify2 = -643586287 1872,03 MB/s @Jorg3000: no change at all ---------------------------------------------------------------------- LV · 2025-05-28 20:36 Perhaps someone will find this topic interesting.  I compare implementations of a 1D linear transport equation solver using the explicit upwind scheme: Domain: 1.0 (discretized into 1,000,000 grid points) Wave speed: c = 1.0 CFL condition: 0.9 Simulation time: 0.01. Win11; i7-8700 Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ----------- --------------------------------------------------------------          | GFortan & OpenMP   |    C++ & OpenMP    | FPC & TThread   |          |  (GCC 14.2.0 -O3)  |   (GCC 14.2.0 -O3) |   3.2.2 (-O3)   |---- ---------------------------------------------------------------------E xTime,  s  |       10.7         |          10.7      |      10.3     |--------------------------------------------------------------------- ----  Source codes in the appendix. ---------------------------------------------------------------------- LV · 2025-05-31 11:48 Today, I switched to a laptop with an i7-12700H processor and ran the task from the previous post with an increased simulation time of 0.1. The results puzzled me - FPC is twice as fast as C++ GCC.  output (C++ & OpenMP GCC 14.2.0 -O3): Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Total steps: 111112Execution time: 82.014 secondsPress Enter to exit...  output (FPC & TThread 3.2.2 -O3): Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Enter number of threads: 16Total steps: 111112Execution time: 38.69 secondsPress Enter to exit... ---------------------------------------------------------------------- srvaldez · 2025-05-31 13:51 my times Code: [Select]fortran Total steps: 111112 gfortran    Execution time:    115.232 seconds, 92.776 seconds with openmp Intel ifort Execution time:     87.984 seconds Intel ifort Execution time:     48.819 seconds with Qiopenmp cpp Total steps: 111112 g+++  Execution time: 109.546 seconds, 61.624 seconds with openmp Intel Execution time: 89.768 seconds Intel Execution time: 38.194 seconds with Qiopenmp pascal Enter number of threads: 16 Total steps: 111112 Execution time: 61.19 seconds ---------------------------------------------------------------------- marcov · 2025-05-31 15:03 Quote from: LV on May 31, 2025, 01:48:47 pmToday, I switched to a laptop with an i7-12700H processor and ran the task from the previous post with an increased simulation time of 0.1. The results puzzled me - FPC is twice as fast as C++ GCC.  It might be the difference of the  thread pool system based on recreating vs recycling worker threads. That might have some influence on which core  a job is initially scheduled ---------------------------------------------------------------------- Thaddy · 2025-05-31 19:31 What puzzles me is that I do not immediately see optimization settings. Especially on the Pascal side -CfXXX and -Sv As we all know the FPC defaults are really conservative. ---------------------------------------------------------------------- LV · 2025-06-01 16:43 @srvaldez, thank you for testing. Which hardware did you use to obtain these results? @marcov and @Thaddy, I appreciate your helpful advice. There is a lot to consider. ---------------------------------------------------------------------- srvaldez · 2025-06-01 17:24 Quote from: LV on June 01, 2025, 06:43:27 pm@srvaldez, thank you for testing. Which hardware did you use to obtain these results? QuoteOS Name   Microsoft Windows 11 Pro System Type   x64-based PC Processor   Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz, 3600 Mhz, 8 Core(s), 16 Logical Processor(s) ---------------------------------------------------------------------- gues1 · 2025-06-01 19:44 Enter number of threads: 16 Total steps: 11112 Execution time: 2.29 seconds Press Enter to exit... Win11, I9-14900HX- 8 PCores, 16 ECores But with Delphi 12.3, 64 bit app, Release mode, I don't have Lazarus now to test. ---------------------------------------------------------------------- gues1 · 2025-06-01 20:00 Enter number of threads: 16 Total steps: 11112 Execution time: 2.31 seconds Press Enter to exit... Win11, I9-14900HX- 8 PCores, 16 ECores With Lazarus 4.0, FPC 3.2.2, Optiomization -O3 ---------------------------------------------------------------------- gues1 · 2025-06-01 20:09 Enter number of threads: 16 Total steps: 11112 Execution time: 1.67 seconds Press Enter to exit... With Lazarus, forcing the use of P Core with (I have 8 PCore => 16 THREADS): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     SetProcessAffinityMask(getcurrentProcess, $FFFF);  With Delphi the time is: Execution time: 1.87 seconds ---------------------------------------------------------------------- Thaddy · 2025-06-02 06:04 Apart from the optimization settings I referred to, I also miss measurements with the FPC llvm back-end. (Well, I tested that previously and on average fpc-llvm is on a par with any llvm backed compiler.  Which is logical) Don't use the excuse that llvm is not native: from that excuse follows that none of the llvm compilers are native, maybe with the exception of clang. And many in speed tests above may be in fact llvm backed. ---------------------------------------------------------------------- Lenny33 · 2025-06-02 10:50 Quote from: Thaddy on May 18, 2025, 08:03:29 amYou don't get it do you? Like most of the other idiots that "compare" speed. It is ff'ing not the language, it is the compiler optimization and a lot of C++ compilers are slower than fpc. If you do still not understand that, please clean your mouth. Such statements are silly naive. Why so boorish to people who write objective things? Yes, Delphi and Lazarus are not designed for writing high-performance computing code. The final code compiled on modern C/C++ compilers (f.ex. on VC++ or Intel C++) usually runs twice as fast as on Delphi or FPC. Even code compiled on Embarcadero C++ Builder (the same owner of Delphi) usually works one and a half times faster than Delphi (though C++ Builder is obviously not the best C++ compiler). In general I think we want too much from an opensource project. We can make such claims to Delphi as a commercial project and wonder why Embarcadero has not yet been able to hire specialists to optimize the final code. As for Lazarus, we are just grateful that it is what it is. At least its final code is not slower than C#. ---------------------------------------------------------------------- Martin_fr · 2025-06-02 11:08 Quote from: Lenny33 on June 02, 2025, 12:50:07 pmQuote from: Thaddy on May 18, 2025, 08:03:29 amYou don't get it do you? Like most of the other idiots that "compare" speed. It is ff'ing not the language, it is the compiler optimization and a lot of C++ compilers are slower than fpc. If you do still not understand that, please clean your mouth. Such statements are silly naive. Why so boorish to people who write objective things? Yes, Delphi and Lazarus are not designed for writing high-performance computing code. The final code compiled on modern C/C++ compilers (f.ex. on VC++ or Intel C++) usually runs twice as fast as on Delphi or FPC. There are a lot of inaccuracies (or at least potential misunderstandings) in there.... But first of all, yes "Mr Devil smiley" (sorry Thaddy, that is you) could have been way more polite (or at least not aggressive/insulting in any way), and not just the once. No matter what the issue is, and how often it has been iterated. At the basics, when comparing 2 equal implementations, then it is a comparison of compiler. There isn't a "Pascal speed" to some code. There is a speed that you get with FPC, a speed for Delphi, and for any other compiler compiling Pascal. So that kind of comparison (that ends in a time measured) is never about languages. Though of course you can (if you spent time collecting all existing compilers) state that at a specific date the fastest compiler got whatever results. Quote Delphi and Lazarus are not designed for writing high-performance computing code. Ignoring that "Lazarus" isn't the language, and neither the Compiler.... Talking about Pascal and other language, then well a language can be designed for performance, by making it easier to access performance improving feature such as parallel execution. But, then - in most case - you can access those features (like threading) even if the design of the Language does not give you special features to make that easier. And if you do access them, then its back to how fast the code generated by the compiler is. ---------------------------------------------------------------------- Martin_fr · 2025-06-02 11:15 Btw, I find the comparisons in this thread a bit lacking. The may be valid (I have not checked), but they compare (at least) 2 features without providing details on how each individual feature performs. If I read it correct, then all benchmarks were performed multi threading. And well the over all outcome is a valid comparison. But, from that little can be said about the compiler. It's a mix of compiler, rtl and libraries. That is, it is unclear if the faster speed was a result of - better thread management (the rtl/library) - faster execution inside each thread (the compiler generated code) I would think it should be interesting to also know how the benchmarks perform single threaded. It would also be telling to know how the time comparisons go when changing the "size" of the benchmark. If all test are run, with double, tripple or 10 times the data => does the time scale by the same factor? Or do some tests gain/loose performance in comparison to others? ---------------------------------------------------------------------- Lenny33 · 2025-06-02 11:26 Quote from: Martin_fr on June 02, 2025, 01:08:34 pmIgnoring that "Lazarus" isn't the language, and neither the Compiler....Yes, you are right, but Lazarus doesn't work with anything other than FPC. So we all know what we're talking about. Quote from: Martin_fr on June 02, 2025, 01:08:34 pmTalking about Pascal and other language, then well a language can be designed for performance, by making it easier to access performance improving feature such as parallel execution.Parallel computing is not the problem. The problem is that FPC or Delphi, unlike the flagship C++ compilers, obviously do not have an intermediate layer which analytically, not syntactically, looks through the sequence of actions between variables and memory and simply removes unnecessary moves, so it really analytically optimizes actions. I noticed this just by analyzing the final assembly code on the same algorithms. You can do such “optimizations” manually as well. But it is very bad from the point of view of code perception and its purity (for someone else to understand it later). But it is quite obvious that such an analytical layer in the compiler is rather difficult to implement and costs a lot of money (man-hours). ---------------------------------------------------------------------- Martin_fr · 2025-06-02 11:53 Quote from: Lenny33 on June 02, 2025, 01:26:04 pmThe problem is that FPC or Delphi, unlike the flagship C++ compilers, obviously do not have an intermediate layer which analytically, not syntactically, looks through the sequence of actions between variables and memory and simply removes unnecessary moves, so it really analytically optimizes actions. There I misunderstood you, but then I said the text was not that accurate. Because you name "Lazarus" and may (as in maybe) have referred to language design, I responded to that. Above made clear you meant implementation in the compiler. Even in regards to the compiler "design" can have many many meanings.... But yes certain techniques (like DFA) are not or not completely implemented. ---------------------------------------------------------------------- Thaddy · 2025-06-02 12:02 Quote from: Lenny33 on June 02, 2025, 01:26:04 pmParallel computing is not the problem. The problem is that FPC or Delphi, unlike the flagship C++ compilers, obviously do not have an intermediate layer which analytically, not syntactically, looks through the sequence of actions between variables and memory and simply removes unnecessary moves, so it really analytically optimizes actions. and what makes you think you are right? Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fpc -ioREGV ARSTACKFRAMEPEEPHOLELOOPUNROLLTAILRECCSEDFASTRENGTHUSERBPORDERFIELDSFA STMATHREMOVEEMPTYPROCSCONSTPROPUSELOADMODIFYSTOREUNUSEDPARAFORLOOPmiss ing any analytical optimizations? oh, yes, probably whole program optimization: disappointingly to you  we have that too!!!! now, what?  Furthermore there is a llm backend (same speed as all other llvm based compilers). If you use that you have also all linker optimizations, although the internal linker already optimizes and the compiler can control external linker options. ok... a well deserved  for the obviously ill-informed. ---------------------------------------------------------------------- Martin_fr · 2025-06-02 12:09 Quote from: Thaddy on June 02, 2025, 02:02:40 pmand what makes you think you are right? What does make you think that just because the compiler can list a feature by name, that feature is actually fully/mostly/sufficiently- complete implemented? Some may, other may only be partly... ---------------------------------------------------------------------- Lenny33 · 2025-06-02 12:09 Quote from: Martin_fr on June 02, 2025, 01:53:21 pmEven in regards to the compiler "design" can have many many meanings.... But yes certain techniques (like DFA) are not or not completely implemented.Whatever has not yet been realized in FPC and Lazarus, overall this project can be considered a great success. And if to argue alegorically, it is foolish to expect from an SUV the speed of a sports car. A programmer should not fixate on one language and technology. You can write a C++ library for fast high-performance calculations. But at least it is not too smart to write a WEB-site in the same C++. ---------------------------------------------------------------------- Thaddy · 2025-06-02 12:19 Quote from: Martin_fr on June 02, 2025, 02:09:20 pmWhat does make you think that just because the compiler can list a feature by name, that feature is actually fully/mostly/sufficiently-complete implemented? Some may, other may only be partly... All of them are implemented. Only one or two have room for improvement. These are also related. The point is that he talks about missing analytical functionality. That is plain wrong. You can't argue against that. It is a troll. ---------------------------------------------------------------------- Lenny33 · 2025-06-02 12:19 Quote from: Thaddy on June 02, 2025, 02:02:40 pmoh, yes, probably whole program optimization: disappointingly to you  we have that too!!!! now, what?  I would be happy to agree with you. But whatever we did with optimization parameters for FPC, but only VC++ libraries on Win still work much faster. And pesonally I don't see any problem with that. Are you interested in nothing else in your life and profession except Pascal? Then your behavior compares to fanaticism. Studying and being able to use different technologies suitable for a particular task is also a part of professionalism. Being a fanatic of one language or technology is IMHO a stubborn fanaticism. ---------------------------------------------------------------------- LV · 2025-06-02 13:52 I'm a fan of the bike, though.  However, I've now made a sequential run of C++ (VS 2022, Release, maximum speed settings) and Pascal (FPC 3.2.2 -O3) programs. Please take a look at post 21 for the source code. Hardware: laptop with an i7-12700H processor. System: Windows 11. Simulation time: 0.01; 0.1; 0.2; 0.3  C++ Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Total steps: 11112Execution time: 2.653 secondsPress Enter to exit... Total steps: 111112Execution time: 38.798 secondsPress Enter to exit... Total steps: 222223Execution time: 118.275 secondsPress Enter to exit... Total steps: 333334Execution time: 244.510 secondsPress Enter to exit...  Pascal Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Enter number of threads: 20Total steps: 11112Execution time: 2.31 secondsPress Enter to exit... Enter number of threads: 20Total steps: 111112Execution time: 36.20 secondsPress Enter to exit... Enter number of threads: 20Total steps: 222223Execution time: 107.42 secondsPress Enter to exit... Enter number of threads: 20Total steps: 333334Execution time: 201.41 secondsPress Enter to exit... ---------------------------------------------------------------------- Lenny33 · 2025-06-02 15:07 Quote from: LV on May 18, 2025, 04:14:13 pmIf we help the compiler a little and unroll the loops in both programs, the results will be as follows (difference 34%).In 2025, everyone is used to the fact that unrolling and optimizing loops, rearranging and optimizing steps for memory access, inlining subroutine code, and other actions to get the most optimal machine code should be done by an intelligent compiler, not a programmer. Forget the shamanism of manually unrolling loops. It's not your business. In 2025 an intelligent compiler should do it (of cause if we speak about high-perfomance computing). The task of an application programmer is to write the most understandable, beautiful and self-documented code that can be understood both by himself in a couple of months and by another specialist. Writing parts of an application program (not a system or controller program) in assembly language is an extremely unsuccessful approach from the point of view of portability. More then it is almost impossible to know all the peculiarities of modern processors. For example, some assembly commands that everyone used in the 90s-2000s will only slow down program execution now. That's when the next commercial Unreal Engine or MatLab will be rewritten in Delphi or FPC to squeeze maximum performance out of the hardware, that's when you can say that Delphi / FPC are definitely meant for high performance computing. Until then, we will mostly use Lazarus/FPC as a great tool for creating cross-platform desktop interfaces and for writing interfaces to databases. ---------------------------------------------------------------------- LV · 2025-06-02 15:37 Thank you for the interesting lecture. I should head out now and go for a bike ride. ---------------------------------------------------------------------- Thaddy · 2025-06-02 20:19 He is a troll, he won't believe your repeatable results anyway, so yes, maybe a bike ride will be the better option. ---------------------------------------------------------------------- Lenny33 · 2025-06-02 21:11 Quote from: Thaddy on June 02, 2025, 10:19:55 pmHe is a troll, he won't believe your repeatable results anyway, so yes, maybe a bike ride will be the better option. You can think of me as anything you want. But it won't speed up the final code generated by FPC  And nobody in their right mind except Pascal fanatics and enthusiasts of manual optimization or direct assembler will use it for high- performance calculations. ---------------------------------------------------------------------- Seenkao · 2025-06-03 11:36 Quote from: Lenny33 on June 02, 2025, 11:11:51 pmYou can think of me as anything you want. Мы не думаем так, вы себя показываете таким. Все ваши ответы показывают, что головой думать вы не хотите, вы надеетесь на компилятор и пытаетесь высмеять другие компиляторы, в данном случае компилятор FPC. Все давно знают, что он не лучшей. Но не важно, лучший он или нет. Он выполняет свои функции. А вы продолжаете троллить. Продолжайте в том же духе и с вами просто перестанут общаться. Google translate: We don't think so, you show yourself that way. All your answers show that you don't want to think with your head, you rely on the compiler and try to ridicule other compilers, in this case the FPC compiler. Everyone has known for a long time that he is not the best. But it doesn't matter whether it is the best or not. It does its job. And you continue to troll. Continue in the same spirit and people will simply stop communicating with you. ---------------------------------------------------------------------- 440bx · 2025-06-03 12:13 FPC does a few optimizations but, FPC is nowhere near what an optimizing compiler is.  For instance, constant folding is an easy optimization.  So easy that sometimes it is the parser that does it, not even the code generator and, FPC can easily be tripped there. Refer to the attachment.  That program was compiled with -O4 and _all_ individual optimizations (-OoXXX) enabled, yet FPC could not consolidate all the constants being added to Value into a single constant. That's just one example and, there are a large number of similarly simple optimizations that FPC cannot do.  It is NOT the programmer's job to add constants.  The compiler (or any other piece of software for that matter), works for the programmer, the programmer isn't supposed to work for the compiler. In case someone wants to play with it, also attached is the program that produced that output. ---------------------------------------------------------------------- ccrause · 2025-06-03 13:10 Quote from: 440bx on June 03, 2025, 02:13:59 pmFor instance, constant folding is an easy optimization.  So easy that sometimes it is the parser that does it, not even the code generator and, FPC can easily be tripped there.Should be fixed (not tested!) by this commit.  This means of course the fix is currently only available in the development branch. ---------------------------------------------------------------------- Lenny33 · 2025-06-03 13:12 Quote from: Seenkao on June 03, 2025, 01:36:21 pmContinue in the same spirit and people will simply stop communicating with you.Pragmatically minded people are more likely to stop listening to obvious dreamers like you  I didn't start this thread. And before my posts, there were already reasonable answers that the code generated by FPC is far from the fastest in comparison with modern C++ compilers. Do you have nothing better to do than manual optimization? I'm glad for you that you have so much time to waste when modern C++ compilers do it even better for a seconds Personally you have already done manual optimization in another thread, which VC++ did in a couple of seconds, and still VC++ code worked twice as fast because FPC cannot inline large procedures and even with the variability of inlined code. Most people choose an instrument according to its purpose and capabilities. If you need high-performance computing, you don't go to FPC. That's not what FPC is for. At least for its current version. ---------------------------------------------------------------------- 440bx · 2025-06-03 13:25 Quote from: ccrause on June 03, 2025, 03:10:27 pmQuote from: 440bx on June 03, 2025, 02:13:59 pmFor instance, constant folding is an easy optimization.  So easy that sometimes it is the parser that does it, not even the code generator and, FPC can easily be tripped there.Should be fixed (not tested!) by this commit.  This means of course the fix is currently only available in the development branch. Thank you for pointing that out. I'm not happy to write what follows but, it really pushes the limits of what is acceptable to have such a simple deficiency take over 3 years to eliminate and it is still not part of the stable release.   That itself is a problem. One thing I believe would help FPC would be to have it as a policy to have a bug-fixes-only release every year.  That way, there would be a continuously available stable release with important bugs and/or deficiencies fixed. ---------------------------------------------------------------------- Thaddy · 2025-06-03 13:36 I agree with that, but basically it is not rocket science to compile fixes (which is stable too) Problem is again not enough hands to prepare a full distribution for all platforms. And that is maybe inconvenient, but how it works in practice. Otherwise platforms beyond Windows, Linux and MacOS get out of sync... The latter three are less of a problem, but the rest is.... I fully understand the core team. They do not want to drop platforms for a release. ---------------------------------------------------------------------- Seenkao · 2025-06-03 13:38 Quote from: 440bx on June 03, 2025, 02:13:59 pmRefer to the attachment.  That program was compiled with -O4 and _all_ individual optimizations (-OoXXX) enabled, yet FPC could not consolidate all the constants being added to Value into a single constant. И на самом деле это правильное поведение компилятора! Он выполнил задание программиста, а программист задал несколько переменных. При изменении переменных в программе, она будет корректно выполняться. Лично меня, устраивает подобное поведение компилятора! А что меня не устраивает, это то, что когда всем переменным передаётся значение "A", то значение "A" не записывается в регистр и в последующем не передаётся другие переменные просто считывая данные из регистра, а постоянно обращается к памяти считывая значение "A", для того чтоб записать значение в новую переменную (это не постоянное поведение компилятора, но очень частое). ----------------------------------- Google translate: And in fact, this is the correct behavior of the compiler! It has completed the programmer's task, and the programmer has set several variables. When changing variables in the program, it will be executed correctly. Personally, I am satisfied with this behavior of the compiler! But what I am not satisfied with is that when all variables are passed the value "A", the value "A" is not written to the register and subsequently other variables are not passed simply by reading data from the register, but constantly accesses memory reading the value "A" in order to write the value to a new variable (this is not constant behavior of the compiler, but very common). Quote from: Lenny33 on June 03, 2025, 03:12:43 pmDo you have nothing better to do than manual optimization?Поздравляю! Вы в очередной раз подтвердили что вы несёте чушь! ))) Google translate: Congratulations! You have once again confirmed that you are talking nonsense! ))) ---------------------------------------------------------------------- Lenny33 · 2025-06-03 14:07 Quote from: Seenkao on June 03, 2025, 03:38:55 pmPersonally, I am satisfied with this behavior of the compiler!We all here have to put up with the compiler's shortcomings in exchange for cross-platform freebies. But “some dreamers” use it for some reason obviously not for its intended purpose  Quote from: Seenkao on June 03, 2025, 03:38:55 pmCongratulations! You have once again confirmed that you are talking nonsense! )))Do you need to be reminded again of your miserable efforts on manual optimization which resulted in vc++ code running twice as fast anyway?  The programming world is so huge for different tasks, but you personally are stuck on Pascal for everithing and say pathetic excuses and inappropriate insults. ---------------------------------------------------------------------- marcov · 2025-06-03 14:38 Quote from: Lenny33 on June 02, 2025, 11:11:51 pmAnd nobody in their right mind except Pascal fanatics and enthusiasts of manual optimization or direct assembler will use it for high-performance calculations. Ok. Now please go to the Python forums, and explain there why Python has no place in data analytics, AI etc as it is not a high performance calculation language, since itis worse than FPC by orders of magnitude. ---------------------------------------------------------------------- Thaddy · 2025-06-03 14:42 Yes, Python is - regarding performance - merely a user interface to C compiled libraries. ( Or, cheekely, FPC compiled libraries.. ) It defers difficult performant code to others. Anyway, stop this troll and kill the thread. ---------------------------------------------------------------------- Lenny33 · 2025-06-03 15:01 Quote from: marcov on June 03, 2025, 04:38:14 pmOk. Now please go to the Python forums, and explain there why Python has no place in data analytics, AI etc as it is not a high performance calculation language, since itis worse than FPC by orders of magnitude. It has already been answered that it is just glue for C libraries. In our project we use Lazarus only as glue for C++ libraries and certainly for cross-platform desktop, but not for computational tasks (it's all in C++ libraries). ---------------------------------------------------------------------- marcov · 2025-06-03 17:59 Quote from: Lenny33 on June 03, 2025, 05:01:21 pmIn our project we use Lazarus only as glue for C++ libraries and certainly for cross- platform desktop, but not for computational tasks (it's all in C++ libraries). I do machine vision for nearly 19 years now. All computationally heavy, but a few assembler routines here and there, and some manual tuning is enough. Maybe I would need less manual tuning if I did large parts in C++, but for me, the tuning is less than the trouble of a multi language project. Yes, I already have C++ wrapper DLL, but those are static, while otherwise both codebases would be live. ---------------------------------------------------------------------- LV · 2025-06-03 19:39 Fiddled a bit with Pascal code (no assembly inserts). The model Cauchy problem for the transport equation. Simulation time 0.1. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Enter number of threads: 20Total steps: 111112Execution time: 24.36 seconds  Additionally, I added a suggestion from @gues1 (thanks). Quote from: gues1 on June 01, 2025, 10:09:03 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     SetProcessAffinityMask(getcurrentProcess, $FFFF);  Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Enter number of threads: 20Total steps: 111112Execution time: 20.23 seconds  To be on the safe side, I reran the C++ code in Visual Studio 2022 with the OpenMP library connected and the settings /O2 /Ob2 /Oi /Ot /GL /openmp Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Total steps: 111112Execution time: 40.269 secondsPress Enter to exit... ---------------------------------------------------------------------- gues1 · 2025-06-03 20:39 Quote from: LV on June 03, 2025, 09:39:38 pm..... Additionally, I added a suggestion from @gues1 (thanks). Quote from: gues1 on June 01, 2025, 10:09:03 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     SetProcessAffinityMask(getcurrentProcess, $FFFF);  That API limits the use of virtual CPU (THREADS in HyperThreading system) in the application. Every bit map a virtual CPU and if it is equal to one the Application will use only those THREADS. In your case, you have 12 THREADS (Power) so the function should be called with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     SetProcessAffinityMask(getcurrentProcess, $FFF);  Of course you can try if this is better. ---------------------------------------------------------------------- Lenny33 · 2025-06-03 22:05 Quote from: marcov on June 03, 2025, 07:59:57 pmI do machine vision for nearly 19 years now. All computationally heavy, but a few assembler routines here and there, and some manual tuning is enough. Maybe I would need less manual tuning if I did large parts in C++, but for me, the tuning is less than the trouble of a multi language project. Yes, I already have C++ wrapper DLL, but those are static, while otherwise both codebases would be live.It's your choice. All I can say is I respect any choice. But in our case, first of all, we all graduated from universities. And universities are mostly C/C++/Java, not Pascal. Pascal is only for some of us from school years and from 90s-2000s Delphi DB interfaces (mostly for old people 45+). And secondly, we have people with mathematical education. They also prefer C++ for computational algorithms. And thirdly, I have already written that we have different tests and the same algorithms of signal filters and other calculations over large data work approximately twice as fast on modern C++ compilers than on Delphi/FPC. And this is critical for us. In our case it is senseless to manually optimize each algorithm for each system and even more senseless to write assembly language for a particular processor. Especially since we have convinced ourselves in practice that C++ code is faster anyway. But our old interface was in Delphi, so it is much easier and more convenient to translate it to Lazarus than to C++Qt since we don't need any modern decorations in the interface yet. ---------------------------------------------------------------------- gues1 · 2025-06-04 07:38 Hi, I believe that speed is not everything in the choices related to the development of an algorithm / application. There are several arguments that lead to the choice of a language or a mix of languages, and even if the choice is not better for speed maybe it is for other characteristics. If speed were the only element of choice, we would all program in assembler (or rather in machine language directly). Using pre-built and optimized libraries (and I refer to computer vision, a field where I have also been working for over twenty years) in C / C++ is certainly a MUST given the field of application, but the application that in any case must have comparable characteristics in terms of speed because it must orchestrate everything, I always develop it in Pascal. And the reasons are simple: it is the language that is more easily maintainable and extensible, whose compiler creates a monolithic executable without carrying tons of frameworks, where normally if you compile (and you have programmed according to the basic rules) you are practically certain that the application is OK. And also the development of algorithms it is absolutely not certain that the Pascal compiler generates executables MUCH slower than a C++. Some time ago I did a test with a Chinese symmetric encryption algorithm (SM4) testing the application in C++ and its porting in PURE PASCAL, the difference in performance was less than 10%. Maybe for the development of a server application with thousands of accesses per second this difference is fundamental, but for a client application that needs to access "every now and then" the implementation in Pascal can be more than fine. And it is in Pascal!!!! ---------------------------------------------------------------------- marcov · 2025-06-04 08:57 Note that the assembler that I use is not really to micro-optimise a pascal piece of code, it is nearly always to invoke SSE2/AVX for whole image operations. We nearly always use 8-bit, and I found that my own (fairly pedestrian) routines usually outperform public packages as opencv, simply because they are tailor made for the purpose and resolution. The cases that I do use more complex routines I often look at the simdlib repo which is a mix of templates and simd intrinsics. I would like to have that in Pascal, but that has nothing to do with the language. ---------------------------------------------------------------------- gues1 · 2025-06-04 09:21 Quote from: marcov on June 04, 2025, 10:57:00 amNote that the assembler that I use is not really to micro-optimise a pascal piece of code, it is nearly always to invoke SSE2/AVX for whole image operations. We nearly always use 8-bit, and I found that my own (fairly pedestrian) routines usually outperform public packages as opencv, simply because they are tailor made for the purpose and resolution. The cases that I do use more complex routines I often look at the simdlib repo which is a mix of templates and simd intrinsics. I would like to have that in Pascal, but that has nothing to do with the language. I use Halcon's C libraries and I found them exhaustive and fast: up to this point I have not found faster libraries with optimized functions, even though I also use systems from its competitors (including OpenCV). I have also optimized several functions in assembler (within Pascal functions) just for a personal use context. The use of features such as TThreads and Affinity Masks (together with thread priorities) allows for even more advanced optimization, especially in the perspective of using resources to the maximum (see AVX and Core / THREADS). And with Pascal this is really simple and elementary as well as easily extensible and maintainable. ---------------------------------------------------------------------- jwdietrich · 2025-06-15 19:18 A recent video on YouTube demonstrates with a specific example that Pascal generates both fast and efficient code: https://youtu.be/1f-rUh-hUMs?si=15EUx-u2ROYpXx_S ---------------------------------------------------------------------- Thaddy · 2025-06-16 08:45 Btw: it is plain wrong to compare a high level compiled language with assembler optimizations (including intrinsics, which is the same, inlined). There are "rumours" that intrinsics support is being worked on for FPC, but I am not aware of the status. ( and likely CPU specific, which makes it very difficult) ---------------------------------------------------------------------- marcov · 2025-06-16 09:45 Quote from: gues1 on June 04, 2025, 11:21:01 amI use Halcon's C libraries We started out with Euresys (which, back then, came from the same dealer that tries to sell us Halcon). We had a budget sidebusiness for which we developed an own optimized blob, which we later expanded for endless products (we did a lot of linescan work). Back then our own routines were faster than Euresys, but also more specialized.  Euresys back then did some MMX and SSE1 use though, which we didn't. We did some measurement prototyping with external libraries in the decade following (first Euresys, later opencv, used KLT tracking for a while), and reused or reimplemented some routines from the SIMDLIB project on sourceforge (I assume now github/lab).   Mostly it allowed us to test if primitives would work before rolling our own. The last 7-8 years we didn't do that much speed dependent work, except for some proof of concept stuff for tenders. I did make some while image processing routines (like simple erosion/dilation kernel operations and some color related stuff) because I wanted to simulate a project at a greater speed than the real application of the project. In general we try to do only one pass of an image using some primitive, and build the rest of the measurement on top of those found primitives without going back to the image. Currently I'm again working on a quite highspeed tender/project, so I'm dusting off old frameworks and tests. But the measurements are relatively simple (blob and edges), so I don't expect much use for opencv c.s.  Threading is relatively simple (a thread per camera), but with 25 cameras on one system, with a dozen at 300fps (actually at 1000fps, but in 0.1 ms bursts 3 times per second) that is still enough to busy most cores. The assembler doesn't work in FPC because it doesn't (yet) implement the Delphi x86_64 stack frame directives (.savenv/.pushnv) that make rolling quick SSE2 routines easy. The pièce de résistance was Nils Haeck's FFT converted to AVX2, but unfortunately while every element was 5-10 times faster, the whole of it was not. (probably only faster when running from uop cache) Another pet project was rotating 8x8 images (since the mounting orientation of the camera might not be the same as the human looking at the picture): https://www.stack.nl/~marcov/rot8x8.txt Quote and I found them exhaustive and fast: up to this point I have not found faster libraries with optimized functions, even though I also use systems from its competitors (including OpenCV). I have also optimized several functions in assembler (within Pascal functions) just for a personal use context. The use of features such as TThreads and Affinity Masks (together with thread priorities) allows for even more advanced optimization, especially in the perspective of using resources to the maximum (see AVX and Core / THREADS). And with Pascal this is really simple and elementary as well as easily extensible and maintainable. What kind of primitives/routines do you typically use from those libs? ---------------------------------------------------------------------- marcov · 2025-06-16 09:49 Quote from: Thaddy on June 16, 2025, 10:45:35 amBtw: it is plain wrong to compare a high level compiled language with assembler optimizations (including intrinsics, which is the same, inlined). Unless one can, and the other can't. Specially if you must make libraries with broad appeal (many formats etc), stringing your assembler routine together using templates and intrinsics can be very productive. QuoteThere are "rumours" that intrinsics support is being worked on for FPC, but I am not aware of the status. ( and likely CPU specific, which makes it very difficult) No rumours, IIRC Gareth looked into it, but afaik stopped. ---------------------------------------------------------------------- gues1 · 2025-06-16 12:50 Quote from: marcov on June 16, 2025, 11:45:25 amWe started out with ................. What kind of primitives/routines do you typically use from those libs? The most used is definitely the transform on the color planes. Followed by the convolution with the Gaussian derivative. Then the whole basic series of primitives such as segmentation with "rank" or "median" filters. I also use the threshold often. But it depends on the type of activity: one of the last ones kept me busy with both classifiers (knn and mlp) and deep learning (training with Halcon and runtime with Yolo implemented in Delphi / C++, not in Python). I started with OpenCV at the time of Intel (with Borland C++), then I continued for a few more years. When OpenCV took a bad turn (weekly updates were released declared "stable" that were either not stable or brought discontinuity to the code) I switched to Halcon, with a certain reluctance because I knew that I would have "tied my hands and feet" to them. Instead I have to say that it couldn't have been a better experience: stability in the versions, compatible code and with detailed explanations if ALL the incompatibilities with previous versions. By the way, the use of OCX (at the beginning of Halcon) allowed me to develop with a certain "detachment" from the code version and also from the environment. Now that they only export C libraries (in addition to Net) I made a wrapper and let's say I have some "versioning" with $IFDEF but really very little. Normally I use I9 processors with two or three threads per camera (so far used a maximum of 16 cameras at the same time). It was a bit hard to optimize the code (for performance) following their super optimized C libraries with the Intel compiler: the function dynamically performs the choice of the technology to use based on the conditions at that particular moment, now it can use AVX then SSE and then maybe even MMX. ... I finish by specifying that I use Delphi for technological and opportunity reasons. About this and regarding the use of open source development environments (i.e. public domain) and equivalent libraries I have to make you think that for those who have a business like me, the responsibility of using known and "certified" commercial products is an absolute condition. No customer (at least mine which are only industries or other large businesses) wants to have something developed with non-commercial products (and I had already tried with OpenCV, lowering not only the cost of Halcon runtime licenses but also a further percentage). ---------------------------------------------------------------------- Thaddy · 2025-06-16 13:14 OpenCV is just a bundle of intrinsics. Just for many CPU's and GPU's. There is already an FPC abstraction for that. So there is little or no performance to gain. What I propose is focus on native support for 128, 256, 512 bit native support. Then we can also finally drop the dreaded 80 bit FPU code that seems to confuse so many people. But that is no mean feat, although parts on 128 bit is already done. ---------------------------------------------------------------------- MathMan · 2025-06-16 20:28 Quote from: Thaddy on June 16, 2025, 03:14:33 pm... 1 - What I propose is focus on native support for 128, 256, 512 bit native support. 2 - Then we can also finally drop the dreaded 80 bit FPU code that seems to confuse so many people. 3 - But that is no mean feat, although parts on 128 bit is already done. Hello Thaddy, Can you pls explain in more detail how the three above are connected? To me it looks like - 1 - is where marcov already stated above that development of it is currently set aside/suspended. But I then fail to see how - 2 - would follow from an available support of - 1. Or do you mean - 1 - as support for IEEE Std 754 (ideally in the 2018 release) Fp128/Fp256/Fp512? At least then I can understand your statement - 3. Cheers, MathMan ---------------------------------------------------------------------- Thaddy · 2025-06-17 08:32 What I really mean is that 80 bit floats are legacy and 128,256 and 512 bit floats do not have high level support while modern processors support those precisions. It is bloody irritating that some people have the misconception that they need 80 bit floats, whereas modern processors have a much, much higher floating point precision. The whole  "extended" issue would be gone with high level support. Does not need to, but might be, just support for intrinsics. (About the only valid reason I sometimes change to clang or C++) ---------------------------------------------------------------------- MathMan · 2025-06-17 10:02 Quote from: Thaddy on June 17, 2025, 10:32:08 amWhat I really mean is that 80 bit floats are legacy and 128,256 and 512 bit floats do not have high level support while modern processors support those precisions. It is bloody irritating that some people have the misconception that they need 80 bit floats, whereas modern processors have a much, much higher floating point precision. The whole  "extended" issue would be gone with high level support. Does not need to, but might be, just support for intrinsics. (About the only valid reason I sometimes change to clang or C++) Thanks for detailing things. Maybe I can help your "bloody irritation", as it seems to stem from a misunderstanding. You seem to be mixing the, e.g. SSE (128 bit), AVX (256 bit), AVX512 (512 bit) on x86-64, SIMD support of modern CPUs with a direct support of IEEE 754 Fp128 (or higher). There are only very few CPU architectures that actually support Fp128 in hardware (see here https://en.wikipedia.org/wiki/Quadruple- precision_floating-point_format#Hardware_support), and none at all for Fp256 or Fp512. So even if there were support for SIMD intrinsics in FPC the demand for "extended floats"/Fp80 would not go away. ---------------------------------------------------------------------- Thaddy · 2025-06-17 19:42 That is a good sum up