Commit 5ba605c7 by songxinkai

add c++/deque/

parent a1d9f755
#include <deque>
#include <iostream>
using namespace std;
int main(){
deque<int> q;
q.push_back(1);
q.push_back(2);
q.push_back(3);
q.push_front(-1);
q.push_front(-2);
q.push_front(-3);
cout << q.size() << endl;
cout << q.front() << ", " << q.back() << endl;
cout << q.at(0) << ", " << q[q.size()-1] << endl;
q.pop_back();
cout << "q.pop_back();" << endl;
cout << q.size() << endl;
cout << q.front() << ", " << q.back() << endl;
cout << q.at(0) << ", " << q[q.size()-1] << endl;
q.pop_front();
cout << "q.pop_front();" << endl;
cout << q.size() << endl;
cout << q.front() << ", " << q.back() << endl;
cout << q.at(0) << ", " << q[q.size()-1] << endl;
return 0;
}
...@@ -35,6 +35,5 @@ int main(){ ...@@ -35,6 +35,5 @@ int main(){
std::function<int(int, int)> func5 = std::bind(print_sum, 1, std::placeholders::_1, std::placeholders::_2); std::function<int(int, int)> func5 = std::bind(print_sum, 1, std::placeholders::_1, std::placeholders::_2);
func5(55, 1); func5(55, 1);
return 0; return 0;
} }
#include <iostream>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
int main(){
float a;
cin >> a;
cout << a << endl;
cout << int(std::ceil(a)) << endl;
}
#include <iostream>
#include <memory>
int main(){
std::shared_ptr<int> a;
a = std::make_shared<int>(10);
std::cout << *a << std::endl;
// /* error when compiling. */
// std::shared_ptr<int> b;
// b = new int;
// std::cout << *b << std::endl;
return 0;
}
#include <iostream>
using namespace std;
int main(){
struct A {
int a0, a1, a2;
float b0, b1, b2;
} B[2] = {1,2,3,1.1,2.2,3.3}, C;
cout << B[0].a0 << ", "
<< B[0].a1 << ", "
<< B[0].a2 << ", "
<< B[0].b0 << ", "
<< B[0].b1 << ", "
<< B[0].b2 << ", "
<< B[1].a0 << ", "
<< B[1].a1 << ", "
<< B[1].a2 << ", "
<< B[1].b0 << ", "
<< B[1].b1 << ", "
<< B[1].b2 << ", "
<< B[2].a0 << ", "
<< B[2].a1 << ", "
<< B[2].a2 << ", "
<< B[2].b0 << ", "
<< B[2].b1 << ", "
<< B[2].b2 << ", "
<< C.a0 << endl;
return 0;
}
#include <cuda_runtime.h>
#include <iostream>
using std::cout;
using std::endl;
#define CUDA_CHECK(x) \
{ cudaError_t cuda_error = x; \
if (cuda_error != cudaSuccess) \
cout << "cudaError_t: " << cuda_error << " != 0 " \
<< cudaGetErrorString(cuda_error) << endl; \
}
#define VECTOR_PRINT(head_str, vec, len) \
cout << head_str << ": {"; \
for (int i = 0; i < len - 1; ++i){ \
cout << vec[i] << ", "; \
} \
cout << vec[len - 1] << "}" << endl;
#define LEN 1000
// kernel functions
template<typename Dtype>
__global__ void kernel(const int N, const Dtype* a, const Dtype* b, Dtype* c){
__shared__ Dtype sum = 0;
int i = threadIdx.x; // thread index in block
for (int i = threadIdx.x; i < N; i += gridDim.x * blockDim.x){
c[i] = a[i] + b[i];
}
}
int main(){
// host memory malloc & initial
int* host_a = new int[LEN];
int* host_b = new int[LEN];
int* host_c = new int[LEN];
for (int i = 0; i < LEN; ++i){
host_a[i] = i;
host_b[i] = i * 100;
host_c[i] = -1;
}
// GPU device start
int device_id = 1;
CUDA_CHECK(cudaSetDevice(device_id));
cout << "Using GPU " << device_id << "." << endl;
// cudaMalloc & cudaMemcpy & cudaMemset
int* dev_a;
int* dev_b;
int* dev_c;
CUDA_CHECK(cudaMalloc((void**)&dev_a, LEN * sizeof(int)));
CUDA_CHECK(cudaMalloc((void**)&dev_b, LEN * sizeof(int)));
CUDA_CHECK(cudaMalloc((void**)&dev_c, LEN * sizeof(int)));
CUDA_CHECK(cudaMemcpy(dev_a, host_a, LEN * sizeof(int), cudaMemcpyHostToDevice));
CUDA_CHECK(cudaMemcpy(dev_b, host_b, LEN * sizeof(int), cudaMemcpyHostToDevice));
CUDA_CHECK(cudaMemset(dev_c, 0, LEN * sizeof(int))); // Set value by byte
dim3 grid_dim(1, 1, 1); // gridDim.x, gridDim.y, gridDim.z (always 1)
dim3 block_dim(16, 1, 1); // blockDim.x, blockDim.y, blockDim.z
kernel<int><<<grid_dim, block_dim>>>(LEN, dev_a, dev_b, dev_c);
CUDA_CHECK(cudaMemcpy(host_c, dev_c, LEN * sizeof(int), cudaMemcpyDeviceToHost));
// VECTOR_PRINT("results", host_c, LEN);
// Free gpu memory & free cpu memory
CUDA_CHECK(cudaFree(dev_a));
CUDA_CHECK(cudaFree(dev_b));
CUDA_CHECK(cudaFree(dev_c));
delete[] host_a;
delete[] host_b;
delete[] host_c;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment