int i;
int n = 1000000;
double area = 0;
double h = 1.0 / n;
#pragma omp parallel shared(n, h)
{
double thread_area = 0; // Private / local variable
#pragma omp for
for (i = 1; i <= n; i++)
{
double x = h * (i - 0.5);
thread_area += (4.0 / (1.0 ...
double area;
double h = 1.0 / n;
#pragma omp parallel for shared(n, h, area)
for (i = 1; i <= n; i++)
{
double x = h * (i - 0.5);
#pragma atomic
area += (4.0 / (1.0 + x*x));
}
pi = h * area;
In this example, each threads execute a subset of the iteration count and they accumula...
double area;
double h = 1.0 / n;
#pragma omp parallel for shared(n, h, area)
for (i = 1; i <= n; i++)
{
double x = h * (i - 0.5);
#pragma omp critical
{
area += (4.0 / (1.0 + x*x));
}
}
double pi = h * area;
In this example, each threads execute a subset of the iteratio...
int i;
int n = 1000000;
double area = 0;
double h = 1.0 / n;
#pragma omp parallel for shared(n, h) reduction(+:area)
for (i = 1; i <= n; i++)
{
double x = h * (i - 0.5);
area += (4.0 / (1.0 + x*x));
}
pi = h * area;
In this example, each threads execute a subset of the iteration...
Include using System.Numerics and add a reference to System.Numerics to the project.
using System;
using System.Numerics;
namespace Euler_25
{
class Program
{
static void Main(string[] args)
{
BigInteger l1 = 1;
BigInteger l2 = 1;
...
RAISERROR function will generate error in the TRY CATCH block:
DECLARE @msg nvarchar(50) = 'Here is a problem!'
BEGIN TRY
print 'First statement';
RAISERROR(@msg, 11, 1);
print 'Second statement';
END TRY
BEGIN CATCH
print 'Error: ' + ERROR_MESSAGE();
END CATCH
RAISERROR ...
RAISERROR with severity (second parameter) less or equal to 10 will not throw exception.
BEGIN TRY
print 'First statement';
RAISERROR( 'Here is a problem!', 10, 15);
print 'Second statement';
END TRY
BEGIN CATCH
print 'Error: ' + ERROR_MESSAGE();
END CATCH
After RAISER...
You can re-throw error that you catch in CATCH block using TRHOW statement:
DECLARE @msg nvarchar(50) = 'Here is a problem! Area: ''%s'' Line:''%i'''
BEGIN TRY
print 'First statement';
RAISERROR(@msg, 11, 1, 'TRY BLOCK', 2);
print 'Second statement';
END TRY
BEGIN CATCH
print...
You can throw exception in try catch block:
DECLARE @msg nvarchar(50) = 'Here is a problem!'
BEGIN TRY
print 'First statement';
THROW 51000, @msg, 15;
print 'Second statement';
END TRY
BEGIN CATCH
print 'Error: ' + ERROR_MESSAGE();
THROW;
END CATCH
Exception with be ...
The following example code is slower than it needs to be :
Map<String, String> map = new HashMap<>();
for (String key : map.keySet()) {
String value = map.get(key);
// Do something with key and value
}
That is because it requires a map lookup (the get() method) for each ...
The Java Collections Framework provides two related methods for all Collection objects:
size() returns the number of entries in a Collection, and
isEmpty() method returns true if (and only if) the Collection is empty.
Both methods can be used to test for collection emptiness. For example:
C...
MySQL does not support the FULL OUTER JOIN, but there are ways to emulate one.
Setting up the data
-- ----------------------------
-- Table structure for `owners`
-- ----------------------------
DROP TABLE IF EXISTS `owners`;
CREATE TABLE `owners` (
`owner_id` int(11) NOT NULL AUTO_INCREMENT,...
Let's say you have a library that returns callbacks, for example the fs module in NodeJS:
const fs = require("fs");
fs.readFile("/foo.txt", (err, data) => {
if(err) throw err;
console.log(data);
});
We want to convert it to a promise returning API, with bluebird - ...
You can convert a single function with a callback argument to a Promise-returning version with Promise.promisify, so this:
const fs = require("fs");
fs.readFile("foo.txt", (err, data) => {
if(err) throw err;
console.log(data);
});
becomes:
const promisify = requ...
In order to convert any callback API to promises assuming the promisify and promisifyAll version doesn't fit - you can use the promise constructor.
Creating promises generally means specifying when they settle - that means when they move to the fulfilled (completed) or rejected (errored) phase to i...
var firstItem = fetch("/api1").then(x => x.json());
var secondItem = fetch("/api2").then(x => x.json());
Promise.all([firstItem, secondItem]).spread((first, second) => {
// access both results here, both requests completed at this point
});
#include <stdio.h>
#include <math.h>
#include <omp.h>
#define N 1000000
int main() {
double sum = 0;
double tbegin = omp_get_wtime();
#pragma omp parallel for reduction( +: sum )
for ( int i = 0; i < N; i++ ) {
sum += cos( i );
}
...
program typical_loop
use omp_lib
implicit none
integer, parameter :: N = 1000000, kd = kind( 1.d0 )
real( kind = kd ) :: sum, tbegin, wtime
integer :: i
sum = 0
tbegin = omp_get_wtime()
!$omp parallel do reduction( +: sum )
do i = 1, N
sum = ...
On a 8 cores Linux machine using GCC version 4.4, the C codes can be compiled and run the following way:
$ gcc -std=c99 -O3 -fopenmp loop.c -o loopc -lm
$ OMP_NUM_THREADS=1 ./loopc
Computing 1000000 cosines and summing them with 1 threads took 0.095832s
$ OMP_NUM_THREADS=2 ./loopc
Computing 100...
Ω-notation is used for asymptotic lower bound.
Formal definition
Let f(n) and g(n) be two functions defined on the set of the positive real numbers. We write f(n) = Ω(g(n)) if there are positive constants c and n0 such that:
0 ≤ c g(n) ≤ f(n) for all n ≥ n0.
Notes
f(n) = Ω(g(n)) means that f(n)...