namespace std has no member image? - dpc++

#include <chrono>
#include <algorithm>
#include <CL/sycl.hpp>
using namespace sycl;
using namespace std::chrono;
using namespace std;
#include <iostream>
#include <fstream>
#include <sstream>
static const int x = 250;
static const int y = 250;
int main()
{
queue q; //create queue
std::cout << "Device: " << q.get_device().get_info<info::device::name>() << std::endl;
std::ofstream image;
image.open("Saltpepper.pgm");
//while open
if (image.is_open()) {
//header info
image << "P3" << std::endl;
image << "250 250" << std::endl;
image << "255" << std::endl;
//to do parallel execution ?
q.parallel_for(range<1>(x), [=](id<1> i) {
std::image << (x * y) % 255 << " " << (x * y) % 255 << " " << (x * y) % 255 << std::endl;
}
std::image.close(); //
}
}

if u want to access your image file you can use them in the following manner
/ Data is array of floats
std::vector<float> v(10000);
// User defines new operator << for std::vector<float> type
std::ofstream& operator << (std::ofstream & str, std::vector<float> & vec)
{
// User’s output actions
...
}
...
// Output file declaration – object of standard ofstream STL class
std::ofstream external_file(“output.txt”);
...
// Output operations
external_file << v;
You can follow the below link for more reference
https://software.intel.com/content/www/us/en/develop/documentation/oneapi-dpcpp-cpp-compiler-dev-guide-and-reference/top/compiler-reference/libraries/intel-s-c-asynchronous-i-o-extensions-for-windows-operating-systems/intel-s-c-asynchronous-i-o-class-for-windows-operating-systems/example-for-using-async-class-template-class.html

Related

Forwarding rvalue into a thread where it is further forwarded to another function

I'm trying to create a deadline class which starts a thread in its constructor. Once the deadline (a time priod) occur within a thread, a function provided as argument should be executed.
The class'es contructor takes that function as a parameter, as well as arguments of that function.
I can provide parameter by value and by reference, but when providing rvalue as parameter, I get compile error, which I don't know how to solve.
#include <thread>
#include <atomic>
#include <future>
#include <iostream>
#include <iomanip>
namespace {
std::atomic<bool> running{true};
template <typename Period>
using duration = std::chrono::duration<int64_t, Period>;
template <typename PromiseType = void>
class deadline {
std::promise<PromiseType> stopper_{};
std::thread thread_{};
public:
deadline() = delete;
template <typename Period, typename Function, typename... Args>
explicit deadline(const duration<Period> &interval, Function&& func, Args&&... args) noexcept {
if (interval.count() > 0) {
std::future<PromiseType> future{ stopper_.get_future() };
using tuple = std::tuple<std::decay_t<Args>...>;
auto decay_copied = std::make_unique<tuple>(std::forward<Args>(args)...);
thread_ = std::thread(
[](std::future<PromiseType>&& future,
const duration<Period> &interval,
Function&& func, decltype(decay_copied)&& params) {
if (future.wait_for(interval) == std::future_status::timeout) {
std::apply(func, *params);
}
}, std::move(future), std::cref(interval), std::forward<Function>(func), std::move(decay_copied));
}
}
deadline(const deadline&) = delete;
deadline(deadline&&) = delete;
deadline& operator=(const deadline&) = delete;
deadline& operator=(deadline&&) = delete;
~deadline() {
stopper_.set_value();
if (thread_.joinable()) {
thread_.join();
}
}
};
}
auto main() -> int {
std::string test1{"test 1"};
std::string test2{"test 2"};
std::string test3{"test 3"};
std::string test4{"test 4"};
using namespace std::chrono_literals;
deadline dl(5s,
[](
std::string test_1
, const std::string& test_2
, std::string& test_3
, std::string&& test_4
) {
std::cout << test_1 << '\n';
std::cout << test_2 << '\n';
std::cout << test_3 << '\n';
test_3 += " modified";
std::cout << test_4 << '\n';
running = false;
}
, test1
, std::cref(test2)
, std::ref(test3)
, std::move(test4)
//, "test 4"
);
while (running.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << test1 << '\n';
std::cout << test2 << '\n';
std::cout << test3 << '\n';
std::cout << test4 << '\n';
return 0;
}
The code could be checked here also:
https://godbolt.org/z/fx3a4szoY
Any help is appreciated.
Thanks!
When providing a const value (also a rvalue), it works, it doesn't work if I use std::move.

ability to run a packaged task with std::bind function parameters in a seperate thread via template function

The question i have is the line where i indicate ERROR below gives error as std::thread constructor works out the function to be invoked and requires
the parameter as needed by the function signature.
Is there any way to solve this ? if i attempt to decode the function name and argument list from packaged_task then i cant use the get_future function over packaged task and need to add my own promise/future code to handle this.
#include<iostream>
#include<string>
#include<thread>
#include<future>
#include<functional>
using namespace std;
int sampleAddFunction(const int& a, const int& b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename T> T asyncExecutor(std::packaged_task<T(T, T)>&& package)
{
std::future<T> result = package.get_future();
std::thread task_td(std::move(package)); // ERROR here as the std::thread identifies the function name from package and requires the params to be passed. How to handle this ?
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
// Executing via directly calling through main.
int testResult1 = sampleAddFunction(100, 200);
cout << "testResult1 = " << testResult1 << endl;
// Attempt to create a std::packaged_task and then run it in another thread.
std::packaged_task<int(int,int)> task(std::bind(sampleAddFunction, 10, 20));
std::future<int> result = task.get_future();
std::thread t(std::move(task), 100, 200); // 100 and 200 are dummy parameters.
t.join();
int testResult2=result.get();
cout << "testResult2 = " << testResult2 << endl;
// Attempt to run this in seperate thread and get results.
std::packaged_task<int(int,int)> task2(std::bind(sampleAddFunction, 15, 27));
int testResult3 = asyncExecutor<int>(std::move(task2), 100, 200);
cout << "testResult3 = " << testResult3 << endl;
}
This should work.
#include<iostream>
#include<string>
#include<thread>
#include<future>
#include<functional>
using namespace std;
int sampleAddFunction(int a, int b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename R, typename F, typename... Ts>
R asyncExecutor(F&& package, Ts... args)
{
std::future<R> result = package.get_future();
std::thread task_td(std::move(package), args...);
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
std::packaged_task<int(int,int)> task2(sampleAddFunction);
int testResult3 = asyncExecutor<int>(std::move(task2), 15, 27);
cout << "testResult3 = " << testResult3 << endl;
}
You are constructing a binary packaged_task (std::packaged_task<int(int,int)>) from a nullary function, the result of your bind (std::function<int()>).
You should either not use bind, or have asyncExecutor accept a nullary packaged_task (std::packaged_task<T()>)
int sampleAddFunction(const int& a, const int& b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename T, typename ... ARGS> T asyncExecutor(std::packaged_task<T(ARGS...)>&& package, ARGS ... args)
{
std::future<T> result = package.get_future();
std::thread task_td(std::move(package), args...);
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
std::packaged_task<int()> task(std::bind(sampleAddFunction, 10, 20));
int testResult = asyncExecutor(std::move(task));
cout << "testResult = " << testResult << endl;
std::packaged_task<int(int,int)> task2(sampleAddFunction);
int testResult2 = asyncExecutor(std::move(task2), 15, 27);
cout << "testResult2 = " << testResult2 << endl;
}

Looping back again to Start

now I'm Having problem in repeating the loop after it finished doing the first and i want to try it again without exiting the program? I've been using while loop to do it but still no joy. so i decided to do the if statement. But the Array only accept 4 strings then it exit. Any one who can help? TIA.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
template <typename T>
void GetContents(T& Input);
template <typename T>
void DisplayContents(const T& Input);
int main()
{
int PASS = 0;
// To Display the unsorted and sorted Book Titles
std::vector<std::string> books;
GetContents(books);
std::cout << "\nMy original library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::sort(books.begin(), books.end());
std::cout << "\nMy sorted library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::cout << "Press 1 to try again, else to quit: ";
std::cin >> PASS;
std::cout << "\n";
if (PASS == 1)
{
GetContents(books);
}
else
{
return 0;
}
// to input All book titles
template <typename T>
void GetContents(T& Input)
{
const int MAX = 5;
string bookName;
std::cout << "Enter a Book Titles:\n> ";
for (int i = 0; i < MAX; i++)
{
std::getline(std::cin, bookName);
Input.push_back(bookName);
std::cout <<">";
}
}
//Display All input book titles
template <typename T>
void DisplayContents(const T& Input)
{
for (auto iElement : Input)
{
std::cout << iElement << '\n';
}
std::cout << '\n';
system("pause");
}

Prevent opening file in "rw" and "r" at the same time

I'm writing class to r/w files from OS file system or from my own archives format in my game engine. How can I make impossible to open file by std::fopen() or std::fstream in modes "rw" and "r". I have written some code to test that on Linux. Here's it:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream in("file.txt", ios::in | ios::out);
if(!in.is_open())
{
cout << "Plik nie może być otwarty w trybie rw" << endl;
return 1;
}
cout << "Plik otwarty w trybie rw" << endl;
in << ".test.";
cout << "Wpisano tekst" << endl;
while(1){}
return 0;
}
/* Drugi plik */
/* The second src code */
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream in("file.txt", ios::in);
if(!in.is_open())
{
cout << "Plik nie może być otwarty w trybie r" << endl;
return 1;
}
cout << "Plik otwarty w trybie r" << endl;
cout << in << endl;
return 0;
}
When I've executed the ./rw program and some instances of ./r, the ./rw has gone into endless loop and the instances of ./r have terminated with 0 code.
Sorry for my English. :)
You should "lock" the file using lockf(): http://man7.org/linux/man-pages/man3/lockf.3.html

Why this small c++11 multi threaded program giving segmentation fault

why this program giving seg fault. I tried figuring out the issue using gdb, but no luck.
#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std;
condition_variable cv;
mutex cv_m;
mutex m;
int count = 0;
#define COUNT_DONE 10
#define COUNT_HALT1 3
#define COUNT_HALT2 6
void functionCount1()
{
for(;;)
{
m.lock();
count++;
cout << "Counter value functioncount1: " << count << endl;
m.unlock();
if(count >= COUNT_DONE)
return;
}
}
void functionCount2()
{
for(;;)
{
m.lock();
count++;
cout << "Counter value functionCount2: " << count << endl;
m.unlock();
if(count >= COUNT_DONE) return;
}
}
int main()
{
thread t1(functionCount1), t2(functionCount2);
t1.join();
t2.join();
return 0;
}
Your program has undefined behavior: the accesses to count outside the mutex in functionCount1 and functionCount2 are data races. With the UB corrected, it seems fine:
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
mutex m;
int count = 0;
#define COUNT_DONE 10
void functionCount(const char* name)
{
for(;;)
{
m.lock();
auto c = ++count;
m.unlock();
cout << "Counter value " << name << ": " << c << endl;
if(c >= COUNT_DONE)
return;
}
}
int main()
{
thread t1(functionCount, "functionCount1"), t2(functionCount, "functionCount2");
t1.join();
t2.join();
}
or if you want to be "clever" and confuse your code reviewers:
void functionCount(const char* name)
{
for(;;)
{
auto c = (std::lock_guard<std::mutex>(m), count++);
cout << "Counter value " << name << ": " << c << endl;
if(c >= count_done)
break;
}
}

Resources