sp1_sdk/cuda/
builder.rs

1//! # CPU Prover Builder
2//!
3//! This module provides a builder for the [`CpuProver`].
4
5use sp1_cuda::MoongateServer;
6use sp1_prover::SP1Prover;
7
8use crate::utils::setup_memory_usage_monitoring;
9
10use super::CudaProver;
11
12/// A builder for the [`CudaProver`].
13///
14/// The builder is used to configure the [`CudaProver`] before it is built.
15#[derive(Debug, Default)]
16pub struct CudaProverBuilder {
17    moongate_server: Option<MoongateServer>,
18}
19
20impl CudaProverBuilder {
21    /// Uses an external Moongate server with the provided endpoint.
22    ///
23    /// # Details
24    /// Run the CUDA prover with the provided endpoint for the Moongate (GPU prover) server.
25    /// Enables more customization and avoids `DinD` configurations.
26    ///
27    /// # Example
28    /// ```rust,no_run
29    /// use sp1_sdk::ProverClient;
30    ///
31    /// let prover = ProverClient::builder().cuda().server("http://...").build();
32    /// ```
33    #[must_use]
34    pub fn server(self, endpoint: &str) -> ExternalMoongateServerCudaProverBuilder {
35        ExternalMoongateServerCudaProverBuilder { endpoint: endpoint.to_string() }
36    }
37
38    /// Allows to customize the embedded Moongate server.
39    ///
40    /// # Details
41    /// The builder returned by this method allow to customize the embedded Moongate server port and
42    /// visible device. It is therefore possible to instantiate multiple [`CudaProver`s], each one
43    /// linked to a different GPU.
44    ///
45    /// # Example
46    /// ```rust,no_run
47    /// use sp1_sdk::ProverClient;
48    ///
49    /// let prover = ProverClient::builder().cuda().local().port(3200).build();
50    /// ```
51    #[must_use]
52    pub fn local(self) -> LocalMoongateServerCudaProverBuilder {
53        LocalMoongateServerCudaProverBuilder::default()
54    }
55
56    /// Builds a [`CudaProver`].
57    ///
58    /// # Details
59    /// This method will build a [`CudaProver`] with the given parameters.
60    ///
61    /// # Example
62    /// ```rust,no_run
63    /// use sp1_sdk::ProverClient;
64    ///
65    /// let prover = ProverClient::builder().cuda().build();
66    /// ```
67    #[must_use]
68    pub fn build(self) -> CudaProver {
69        CudaProver::new(SP1Prover::new(), self.moongate_server.unwrap_or_default())
70    }
71}
72
73/// A builder for the [`CudaProver`] with an external Moongate server.
74///
75/// This is not meant to be used directly. Use [`CudaProverBuilder::server`]
76/// instead.
77#[derive(Debug)]
78pub struct ExternalMoongateServerCudaProverBuilder {
79    endpoint: String,
80}
81
82impl ExternalMoongateServerCudaProverBuilder {
83    /// Builds a [`CudaProver`].
84    ///
85    /// # Details
86    /// This method will build a [`CudaProver`] with the given parameters.
87    ///
88    /// # Example
89    /// ```rust,no_run
90    /// use sp1_sdk::ProverClient;
91    ///
92    /// let prover = ProverClient::builder().cuda().server("http://...").build();
93    /// ```
94    #[must_use]
95    pub fn build(self) -> CudaProver {
96        CudaProver::new(SP1Prover::new(), MoongateServer::External { endpoint: self.endpoint })
97    }
98}
99
100/// A builder for the [`CudaProver`] with the embedded Moongate server.
101///
102/// This is not meant to be used directly. Use [`CudaProverBuilder::local`]
103/// instead.
104#[derive(Debug, Default)]
105pub struct LocalMoongateServerCudaProverBuilder {
106    visible_device_index: Option<u64>,
107    port: Option<u64>,
108}
109
110impl LocalMoongateServerCudaProverBuilder {
111    /// Sets the embedded Moongate server port.
112    ///
113    /// If not set, the default value is `3000`.
114    #[must_use]
115    pub fn port(mut self, port: u64) -> Self {
116        self.port = Some(port);
117        self
118    }
119
120    /// Sets the embedded Moongate visible device index.
121    ///
122    /// If not set, the default value is `3000`.
123    #[must_use]
124    pub fn visible_device(mut self, index: u64) -> Self {
125        self.visible_device_index = Some(index);
126        self
127    }
128
129    /// Builds a [`CudaProver`].
130    ///
131    /// # Details
132    /// This method will build a [`CudaProver`] with the given parameters.
133    ///
134    /// # Example
135    /// ```rust,no_run
136    /// use sp1_sdk::ProverClient;
137    ///
138    /// let prover = ProverClient::builder().cuda().local().visible_device(2).port(3002).build();
139    /// ```
140    #[must_use]
141    pub fn build(self) -> CudaProver {
142        setup_memory_usage_monitoring();
143        CudaProver::new(
144            SP1Prover::new(),
145            MoongateServer::Local {
146                visible_device_index: self.visible_device_index,
147                port: self.port,
148            },
149        )
150    }
151}